简体   繁体   English

通过MAT了解内存泄漏

[英]understanding memory leaks by MAT

I am new to android programming. 我是android编程的新手。 The memory consumption of may android app increases significantly over time. 随着时间的推移,可能会增加android应用的内存消耗。 When analyzed through MAT, it shows the objects being piled up whose GC root is Native Stack . 通过MAT分析时,它显示了GC根为Native Stack正在堆积的对象。 Those objects are being referenced as global ref in native code, but properly being released over time I have also put logs to make sure the count matches. 这些对象在本机代码中被引用为全局ref,但是随着时间的流逝,这些对象会被适当地释放,我还放置了日志以确保计数匹配。 The documentation about native stack is not much clear as it just states: 关于native stack的文档并不十分清楚,因为它仅指出:

In or out parameters in native code, such as user defined JNI code or JVM internal code. 本机代码中的输入或输出参数,例如用户定义的JNI代码或JVM内部代码。 This is often the case as many methods have native parts and the objects handled as method parameters become GC roots. 通常是这种情况,因为许多方法具有本机部分,并且随着方法参数而处理的对象成为GC根。 For example, parameters used for file/network I/O methods or reflection. 例如,用于文件/网络I / O方法或反射的参数。

I am not quite sure what it says, where is the problem and how can I fix it. 我不太确定它说的是什么,问题在哪里以及如何解决。 Any hints are much appreciated. 任何提示都非常感谢。 Thanks in advance. 提前致谢。

This answer won't give you a definitive solution, not because I'm not willing, but because it's impossible (and even harder without not just viewing your code, but knowing very well your code). 这个答案不会给您一个确定的解决方案,这不是因为我不愿意,而是因为它是不可能的(并且更困难的是,不仅要查看您的代码,还要非常了解您的代码)。 But from my experience I can tell you that those kind of memory leaks doesn't occur just due to directly referenced objects - objects you declare (and keep referencing another classes/objects) in turn depends on many other classes and so on, and probably you're seeing a memory leak due to an incorrect handling of any of your instances which at the same time reference to other instances. 但是根据我的经验,我不会告诉您那种内存泄漏不会仅由于直接引用的对象而发生-您声明的对象(并继续引用其他类/对象)又取决于许多其他类,依此类推,也许您发现由于对任何实例的错误处理而导致内存泄漏,这些实例同时引用了其他实例。

Debugging memory leaks is a often a very hard work, not just because as I said above it sometimes doesn't depend directly on what you've declared, but also because finding a solution might not be trivial. 调试内存泄漏通常是一项非常艰巨的工作,不仅因为如上所述,有时它并不直接取决于您声明的内容,而且还因为找到解决方案可能并非易事。 The best thing you can do is what you already seem to be doing: DDMS + HPROF. 您可以做的最好的事情就是您似乎已经在做的事情:DDMS + HPROF。 I don't know how much knowledge you have, but although it's not a universal method, this link helped me so much to find memory leaks in my code. 我不知道您有多少知识,但是尽管它不是通用方法,但此链接对我的发现在代码中发现内存泄漏的帮助很大。

Although it seems trivial, the best way to debug those kind of things is progresively remove portions of your code (overall, those which implies working with instances of other classes) and see how the HPROF report change. 尽管看似微不足道,但调试这类事情的最佳方法是逐渐删除代码的某些部分(总体而言,这意味着使用其他类的实例)并查看HPROF报告如何更改。

---- EDIT ---- ----编辑----

This question on SO is a good example to illustrate the GC roots. 关于SO的这个问题是说明GC根源的一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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