"Original" Android memory management-hprof file

Source: Internet
Author: User

Reprint please specify the source http://www.cnblogs.com/weiwangnuanyang/p/5703702.html

If you just want to determine if there is a memory leak in a scene, the Androidstadio console has a good tool that repeatedly checks to see if the curve is up, and if the curve goes up, it indicates a memory leak.

You can also generate a hprof file by clicking the third button next to it. But I'm more accustomed to eclipse, and here's an example of eclipse.

Here is to focus on how to use memory analysis (Eclipse of a plug-in, specific installation method please visit Niang) to generate and analyze the Hprof file.

1. Generate the Hprof file.

Building the HPROF file is simple, in Eclipse's Ddms view, click on the process you need to debug, then click the [DUMP HPROF file] button.

For the memory leak problem is generally need to compare and analyze, so if there is no automated test environment, manual testing can enter the app to save a hprof file,

And then began to repeatedly test the leak scene 20+ times, save a hprof file, and then repeatedly operation 20+ Times, and then save a hprof file.

2. Brief introduction.

The following is an overview view after opening a hprof file.

From this overview view we can see

Details: Shows the total size, classes size, objects size, ClassLoader quantity, unreachable objects histogram (can be recycled object histogram);

Biggest Objects by retained Size (Memory Max object pie chart);

Histrogram (histogram): Lists the number of objects per class;

Dominator Tree: Enumerates the largest objects, and the size of objects that cannot be reclaimed due to classes;

Top consumers (largest consumer): Lists the pie chart of the object that consumes the most memory;

Duplicate Classes: Detects classes loaded by multiple classloader;

Leak Suspects: Includes a simple leak analysis and a system overview;

TOP components: Lists the component in the heap larger than the total size of 1%, sorted by class loader (System class Loader,pathclassloader,dexclassloader);

Component Report: Analyze objects belonging to common root package or class loader.

There are also buttons on the panel that provide the ability to search for objects by address, view thread details, generate reports, and more. Specific people point to see on the OK.

3. Analyze hprof file in real combat.

With my limited experience of analyzing memory leaks, the leak suspects provided by the memories analysis is often inaccurate, and its analysis is based on the total size of the objects that the class occupies in memory,

Always list some system classes such as byte in the first place. Just like this.

So real effective analysis depends on yourself.

To solve the problem of Java memory leak, first of all to understand what in Java will cause memory leaks, specifically, please step http://www.cnblogs.com/weiwangnuanyang/p/5704596.html;

A very effective way to analyze memory problems is to compare the number of classes at the beginning of the test and the end of the test. The reference relationships for these classes are then parsed.

Let's take a look at the following example.

The first step: Generate two files and open, as follows there are two files of 17:55:55 and 18:00:29.

Step Two: Open the class bar of the second file.

This is the case when you open it:

Step Three: Click the comparison button above the histogram to compare it to another document.

And the result is this:

Fourth Step: Enter your package name keyword in the <Regex> input box, filter out your own class, and filter out the system class.

It's very clear that those classes are leaking out. Repeated operations n times to increase the number of n+ classes are mostly problematic.

Part fifth: Analyze the referential relationships of these classes. In this comparison table right click is not effective, you need to open a new histogram view, also use your package name to filter out the class you write.

Then select a right click, this time will come out a menu, the first three items in the menu represents the object's reference relationship, the class's reference relationship, the class's recycling path.

Let's take a look at the merge shortest path to GC Root (Shortest recovery path) for the class, and the result is this:

First you can see there is a large innerreceiver, this is definitely a problem, point open one can see the last of our own class is Bnavigator, click on this class can see it registered

Broadcast but no anti-registration.

Then look at the other objects that are not recycled, because Mapcontroller references the context of carnaviacitity, and Mapcontroller is a map key.

Here are two questions to consider, one is whether Mapcontroller as a key is appropriate, when it is used as a key in time to remove from the map when it is useless, and the other is Mapcontroller reference carnaviactivity

Context, not ApplicationContext, which is a major cause of anroid's activity leaks.

Another look at Surfaceview is also the context of activity.

The analysis of other classes is similar, not listed here.

"Original" Android memory management-hprof file

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: info-contact@alibabacloud.com 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.