The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption
This is very perfect and effective tool to measure the memory consumption and allocation of the android (java) based application. It’s available in form of plug-in and stand alone application which are identical and easy to use. Through this blog I would like to explain few features of MAT that can help developers discover memory leaks. As the memory analyzer tool, it is working with heap dumps; I will explain detailed of heap dumps — what they are, which formats MAT can read, how to install in Eclipse and how to get heap dump for application.
What Is a Heap Dump?
A heap dump is picture of the memory of a java process at particular point of time. This contains different piece of information based on the format it supports and based on that it persists this data. But in general the picture contains information about java objects and classes in the heap at the moment the dump triggered. As this is just a snapshot at a given time, it doesn’t contain the information such as when and at which method an object was allocated.
Heap Dumps useful for
Heap dumps are useful for lot many things 🙂
First and foremost thing is Debugging of OutOfMemoryError Exceptions. Analyzing an automatically written heap dump with MAT can be very easy and fruitful for such kind of exceptions. The overview of the memory footprint of your application can help you to find which your biggest structures are and what redundant data structures are. To find space wasted in unused collection this tool is again good choice.
MAT can help you to dig out the reasons for too many GC logs for your application or want to see which method allocates most of the objects, and then you would need to use a profiler which is collecting data overtime from the VM. Leak detecting techniques relying on analysis of the objects behavior (allocation / garbage collection) are difficult to implement using heap dumps.
Types of Heap Dumps
Currently the Memory Analyzer is able to work with HPROF binary heap dumps for android based application and this can be viewed with plug-in as well as with stand alone desktop applications. This tool supports all the major operating system.
HPROF Binary Heap Dumps Contains
Different components of HPROF report file and brief understanding
- Histogram view: This view shows the list of all the JVM APIs which are used for this application and no of objects allocated at particular time. It also gives information about Shallow and Retained heap which can be useful in debugging of the GC logs.
- Dominator Tree: A tree like structure which shows the API calls with Shallow Heap and Retained Heap and it’s usage in percentages. We can analyze particular API with listing incoming and outgoing object references and Path to GC roots, which can be helpful debugging GC.
- a list of GC roots
- the call stacks of all threads
- Leak suspects
- Top Consumers chart
- Top Component
How to install MAT in Eclipse
Eclipse->Help->Install New Software->Add
Give name as MAT
Then it will ask for License agreement so please accept that license and proceed further.
How to Get a Heap Dump for Android Application
From Eclipse, while the application is up on Emulator/Device, go to DDMS and select the process and press below icon
This will generate HPROF file and if the MAT is installed properly it will open the HPROF file directly into the MAT and this will give you report form of heap dump.