How to analyze android's OOM, and java static code analysis tool

Analysis of OOM with MAT
Many OOMs seem to occur when the bitmap is allocated, but it is generally not the rootcause. The root cause lies in the resources that should be automatically released, because of code errors, some objects have been referenced (Reference), such as Android memory optimization, how to avoid the mContext reference of the Activity mentioned in the OOM article.

When the amount of code is very large, it is very difficult to find errors by reading the code alone, so tools must be used. Here is a very useful analysis tool MAT.

1. Download MAT

http://www.eclipse.org/mat/downloads.php

Generally, Eclipse is selected for our development environment, so you can directly install the plug-in version.

2. For usage, you can read 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

First of all, you must know the operation steps to reproduce OOM. If it is randomly tested, you also need to find an effective reproduction step. Then take the .hprof before the operation, and the .hprof after the operation, after the memory grows. If the memory keeps growing, 3 or 4 times are desirable. Then open the histogram (Histogram) view respectively, in the object list, compare the change of the Retained size of each object.

The first place is not necessarily the leaked object, it is possible that it consumes a lot of memory under normal circumstances.

The leaked object was the one who suddenly rose in the rankings. The method of distinguishing is to look at the memory address of each object, and the same object is the same object (provided that the process has been running without restarting, and the memory address will change after restarting).

After the suspicious object appears, right-click List Objects > with incoming references, you can exclude WeakReference and other references, and search down the tree node. If there is an Activity in the program, or a global object, it can basically be determined that it is not released. . To further analyze why it is not released, if the logic is complicated and difficult to figure out, you can directly do a workaround and find a way to release the object (set object = null).

Java static code analysis tool It is inevitable that there will be omissions in the process of
writing code, we can also use tools to analyze, here are the commonly used java static code analysis tools:

http://www.oschina.net/question/129540_23043

Personally think Find Bugs and PMD That's fine, it's just an assistant, don't rely too much on it, he's not omnipotent, and not all mistakes can be found.

Welcome to reprint: http://www.yinqisen.cn/blog-315.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326603402&siteId=291194637