Analyzing Java memory using the mat

Source: Internet
Author: User

Overview

The MAT (Memory Analyzer Tool) is a Java Heaper parser that can be used to analyze memory leaks and reduce memory consumption. Analyze the memory used by/DEV/ASHMEM/DALVIK-HEAP (deleted) in the process Showmap. You can refer to the article I wrote using SHOWMAP to analyze the system footprint.


here's how to use eclipse mat to analyze the memory consumption of Android applications. What is needed is eclipse, which already has ADT and SDK installed. You can then download the mat plugin on http://www.eclipse.org/mat/to install the mat on eclipse.

as shown, the phone is connected to the computer, open Eclipse, enter DDMS, select a process, you can see the following

Screen:

The update heap key is updated immediately, allowing the tool to get the latest heap allocations for Android apps running on the phone. The dump Hprof key can immediately dump the Hprof file (Java Heap allocation information is stored in the Hprof file). The cause GC key is essentially forcing a GC to be raised (garbage collection).

Click on the dump heap and we'll see the following screen:


This is the main page of the Hprof file analysis interface in Eclipse.

where histogram can see the allocation of all instances, Dominator tree lists the largest objects of the heap. Leak suspects is primarily a list of suspected memory leaks.

Histogram

first, let's take a look at histogram,

This lists all instance types, right-click the instance type Byte, select Merge shortest Paths to GC Root, and select Excluse all Phantom/weak/soft etc refereneces. The Merge shortest Paths to GC root mainly shows the shortest path from GC root. Java's garbage collection mechanism is a bit like the depth traversal of a tree, and if an object has a reference, then the GC root has a path to the object. If the GC root does not have any paths between the objects, the object is inaccessible and can be recycled. Exclude all Phantom/weaker/soft etc references is to exclude virtual references, weak references, and soft references. Because these references can be recycled in general. (Refer to the Deep Java Virtual Machine garbage collection chapter for a detailed explanation).

you can then see the following screen:


You can see that there is a Spreloadeddrawables Longsparsearray object in the resources class, and Spreloadeddrawables has an object array of mvalues. The 139th element of the array is an Niepatchdrawable object that has a Mninepatch object, and Mninepatch has a reference to a mchunk byte array.

Perhaps you have noticed that there is a shallow heap and retained heap in the figure, what does that mean? In short, the shallow Heap size is the size that the object occupies. The retained Heap size is the shallow size of the object itself, shallow size + object directly referenced or indirectly referenced.

The code can then be parsed to see how the byte array is referenced, and when the byte array was created and destroyed, and whether it was created or destroyed is justified.

If the above object is supposed to be a GC off, then what should we do with it? The simple way to do this is to either dispose of it as null or remove it to the GC root without the path reachable, in a non-accessible state, the garbage collector can be recycled.

Because there are many objects, for ease of analysis, we can click the cause GC key to force it to occur once GC, which can reduce the number of objects, so that we analyze.

Leak Suspects

Leak suspects lists the memory leak points that the tool suspects.

In, the Mat tool suspects that the first problem has a 1.5M memory leak:

Then, it is the description of problem one, listing some of the larger instances.

Click Details to see detailed information.

However, the tool may not really exist in doubt, but it provides a reference.

Reference:http://help.eclipse.org/kepler/index.jsp?topic=/org.eclipse.mat.ui.help/welcome.html

http://www.eclipse.org/mat/about/screenshots.php

Http://plumbr.eu/blog/how-much-memory-do-i-need-part-2-what-is-shallow-heap

http://joeylittlefat.diandian.com/post/2011-09-27/5340625

Analyzing Java memory using the mat

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.