How to analyze an oom for Android, with Java Static Code analysis tool

Source: Internet
Author: User

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

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:


3. Focus on understanding retained Heap, GC Root

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:

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://

How to analyze an oom for Android, with Java Static Code analysis tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.