我试图通过 MAT
找出应用程序的内存泄漏,在 list_object
中我找到了一些数值
int Shallow heap
和 retained heap
列。这些值是什么,如何知道内存泄漏在哪里。
最佳答案
来自 Nikita Salnikov-Tarnovski's blog :
The shallow heap is easy – it consists of only the heap occupied by the object itself. There are some nuances to how to calculate it, but for the scope of this article we leave it as is. Stay tuned for future posts on the same topic.
The retained heap is in many ways more interesting. Only rarely are you interested in the shallow heap, in most cases your actual question can be translated to “If I remove this object from the memory, how much memory can now be freed by the garbage collector”.
Now, as we all remember, all Java garbage collection (GC) algorithms follow this logic:
1)There are some objects which are considered “important” by the GC. These are called GC roots and are (almost) never discarded. They are, for example, currently executing method’s local variables and input parameters, application threads, references from native code and similar “global” objects.
2)Any objects referenced from those GC roots are assumed to be in use and hence not discarded by the GC. One object can reference another in different ways in Java, in the most common case an object A is stored in a field of an object B. In such case we say “B references A”.
3)The process is repeated until all objects that can be transitively reached from GC roots are visited and marked as “in use”.
4)Everything else is unused and can be thrown away.
关于Android:浅堆和保留堆有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12707572/