简体   繁体   中英

How to find a memory leak in Java / Android

I am having a lot of trouble with a memory leak in one of our apps and not sure where to look next. I know similar questions have been asked but none had any answers that were helpful.

When we use the profiler in android studio it shows that "native" memory increases quite rapidly, and after using the app for roughly 10-15 minutes it will crash from a memory leak.

Our app is written entirely in java and kotlin, I've looked through for any static object, or objects kept in companion objects that may not be collected by the garbage collector, but there are none other than a few static strings like a TAG for logging.

We tried using leak canary but it can't seem to find any memory leaks and keeps telling us our app is fine. I even intentionally added a leak by holding onto an android activity context statically just to ensure leak canary was working, and it was.

Are there any other things I should look out for that may be causing memory leaks in my java code? Any specific objects that I may be holding onto causing this?

In Android Studio you can generate a heap dump by pressing the Dump Java heap button: 转储 Java 堆按钮

here you can see how much which class is using memory. for example, you might find a class that should have been collected by garbage collector but didn't.

**update: as you stated the problem is with native memory allocation, but I would keep this answer cause it might help someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM