Android:浅堆和保留堆有什么区别

标签 android performance memory-management memory-leaks

我试图通过 MAT 找出应用程序的内存泄漏,在 list_object 中我找到了一些数值 int Shallow heapretained 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/

相关文章:

android - 如何使用 Robotium 单击自定义 ActionbarSherlock View

android - 如何将 ExecutorService 与 Android AsyncTask 一起使用?

r - 为什么在具有许多组的大型数据帧上拆分效率低下?

linq to Db4o 不使用索引

python - 为什么相同值的python变量指向相同的内存地址?

JavaScript,没有引用的对象实例

android - solo.searchText 不识别 "$"

c# - SQL Compact 中用于大型数据集的 SQL Minus

C:将二维数组交给双指针

android - 奇怪的 AndroidViewModel LiveData 观察者行为