Find the class that waste the Memory

2019-04-02 12:24发布

I've published an app several months ago in the Google Play, most of my user have strong problem with the memory usage of my app and that was true, because when I checked my memory usage from "Running Application" Tab, I see that it take 80~110MB of memory, like the below picture:

enter image description here

To find the Class/Activity or pieces of the code that cause this problem I found the MAT (Memory Analysis Tools) plugin useful but it's really make me confused, please take into account to the following image:

enter image description here

The total size of the used memory is 9.8MB however "Running Application" Tab show me 80MB at the same time!

The other problem is Histogram, the Shallow Heap of byte[] object is too high.

Is it normal? Also when I debug some Google project, the byte value always is too high!

enter image description here

So how can I find what is using all this memory?

2条回答
一夜七次
2楼-- · 2019-04-02 12:29

Have you tried using visual vm? Here's a nice tutorial to find out the memory usage of the classes and instances (visual vm tutorial). Hope this helps you.

查看更多
beautiful°
3楼-- · 2019-04-02 12:52

When an Android application starts, it inherits objects allocated by Zygote - a trick to make application spawning faster. So when you get a memory dump they show up as your own.

The answer to why Zygote has that much memory allocation is another trick to make resource loading faster. During Android boot up, Zygote preloads some list of classes and resources so all other application doesn't need to load them every time.

See ZygoteInit.java#330 for implementation of this preloading.

Heavy use of byte arrays are due to this preloaded resources (which are bitmaps).

See this answer for more concrete example of MAT / byte array usage analysis from a definitive resource.

查看更多
登录 后发表回答