Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KSP memory leak in Android build #2073

Closed
nico-gonzalez opened this issue Aug 30, 2024 · 3 comments · Fixed by #2146 · May be fixed by android/nowinandroid#1704
Closed

KSP memory leak in Android build #2073

nico-gonzalez opened this issue Aug 30, 2024 · 3 comments · Fixed by #2146 · May be fixed by android/nowinandroid#1704

Comments

@nico-gonzalez
Copy link

nico-gonzalez commented Aug 30, 2024

Hello! While investigating OOMs in our build we detected that heap space and metaspace is not being reclaimed after the build finishes, and mainly meta space keeps growing with each incremental build we run on both Gradle and Kotlin daemons.
It also seems that when ksp tasks run, the heap and meta space start to grow considerably

Is it possible that there is a memory leak somewhere in KSP?

Here's some info while running the heap dump through Eclipse MAT and Visual VM

Screenshot 2024-08-30 at 2 38 59 PM Screenshot 2024-08-30 at 2 39 13 PM Screenshot 2024-08-30 at 2 39 19 PM Screenshot 2024-08-30 at 2 45 26 PM Screenshot 2024-08-30 at 2 45 17 PM

I can share the heap dump if that's helpful. Let me know if you need anything else from my side to investigate this

@nico-gonzalez nico-gonzalez changed the title KSP memory leak in Andoid build KSP memory leak in Android build Aug 30, 2024
@bnorm
Copy link

bnorm commented Oct 10, 2024

I may have found the root cause of this issue while investigating a reported memory leak in the Kotlin daemon: https://youtrack.jetbrains.com/issue/KT-72169.

I believe the custom implementation of ThreadLocal in KSObjectCacheManager is leaking the class loader due to never being removed from the thread. Adding caches_prop.remove() to the clear() function may be enough to fix this issue.

I also recommend that all other ThreadLocals used within KSP are removed during cleanup as well. I don't think they'll leak the class loader, but they will at least leak a class instance.

@kaeawc
Copy link
Contributor

kaeawc commented Oct 10, 2024

That looks like exactly what I was finding in my heap dumps investigating this issue. Thanks @bnorm !

@eygraber
Copy link

@ting-yuan I've been getting hit by this a lot. Any chance a fix can get into a coming release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants