Analyze Oom with Mat
Many oom seems to occur when bitmap is allocated, but it is generally not rootcause. The root cause is that the resources that should be freed automatically, because of code errors, cause some objects to always be referenced (Reference), such as Android memory optimizer, how to avoid mcontext references to the activity mentioned in the Oom article.
When the code is very large, it is difficult to find errors by reading code alone, so it is necessary to use tools to introduce a very useful analysis tool mat.
1. Download Mat
http://www.eclipse.org/mat/downloads.php
In general our development environment has chosen eclipse, so directly install plug-in version of the can.
2, the use of the method, you can see this blog post:
Http://www.cnblogs.com/Android-and-android/archive/2013/03/05/2943863.html
3. Focus on understanding retained Heap, GC Root
http://blog.csdn.net/hhww0101/article/details/8133219
4. How to locate
The first thing to know is to reproduce the oom procedure, if it is randomly tested, also need to find a valid recurrence step. Then take the. Hprof before the operation, and after the operation, after the memory growth. Hprof. If memory is growing, it is advisable to 3, 4 times. Then open the histogram (histogram) view separately, and in the object list, compare the retained size changes for each object.
The first bit is not necessarily a leaked object, it is possible that it itself is a normal consumption of memory.
The object of the leak was the sudden rise in the rankings. The distinguishing method is to look at the memory address of each object, the same address is the same object (if the process has been running, not restarted, the memory address is changed after the restart).
After the suspect object appears, right-click List Objects > with incoming references, you can exclude the weakreference and other references, follow the tree node down, if there is Activity in the program, or a global object, Basically, you can be sure that it's not released. To further analyze why it is not released, if the logic is complex, difficult to smooth out, you can directly do workaround, to find a way to release this object (set object = null).
Java Static Code analysis tool
There are inevitably omissions in the process of writing code, and we can also use tool analysis, which is a common Java static Code analysis tool:
http://www.oschina.net/question/129540_23043
Personally feel that find Bugs and PMD can be, just auxiliary, not overly dependent, he is not omnipotent, not all errors can be found.
Welcome reprint: Http://www.yinqisen.cn/blog-315.html
How to analyze an oom for Android, with Java Static Code analysis tool