Version 1.1
Copyright © 2009, 2010, 2011, 2012, 2013 Lars Vogel
30.04.2013
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 01.10.2009 | Lars Vogel |
Created |
| Revision 0.2 - 1.1 | 02.10.2009 - 30.04.2013 | Lars Vogel |
|
Table of Contents
The Eclipse MAT tooling is a set of Eclipse plug-ins which visualize the references to objects based on Java heap dumps and provides tools to identify potential memory leaks.
The garbage collector of the JVM releases Java objects from memory as long as no other object refers to this object.
A Java heap dump is a snapshot of the complete Java object graph at a certain point in time. It includes all objects, fields, primitive types and object references.
It is possible to instruct the JVM to create automatically a
heap dump
in case that it runs out of memory, i.e. in case of a
OutOfMemoryError
error. To instruct the JVM to create a
heap dump
in such a situation, start your Java application with the
-XX:+HeapDumpOnOutOfMemoryError
option.
Android allows to create heap dumps of an application's heap. This heap dump is stored in a binary format called HPROF. To create a heap dump us the button in the DDMS Perspective.
The Dalvik format is similar to the Java heap dump format. To convert the Dalvik heap dump to the Java heap format you can use the hprof-conv source target command.
After a new
heap dump
with the
.hprof
ending has
been created, you can open it via a double-click in
Eclipse.
You may need
to
refresh your
project (F5 on the project).
Double-click the file
and
select the
Leak Suspects Report.

The overview page allows you to start the analysis of the heap dump. The dominator tree gives quickly an overview of the used objects.

Install Eclipse MAT via the Eclipse Update manager Select General Purpose Tools and install the Memory Analyzer and Memory Analyzer (Charts) .
Create the Java project called
com.vogella.mat.first
and the
com.vogella.mat.first
package. Create the following class.
package com.vogella.mat.first; import java.util.ArrayList; import java.util.List; public class Main {/** * @param args */public static void main(String[] args) { List<String> list = new ArrayList<String>(); while (1<2){ list.add("OutOfMemoryError soon"); } } }
You can also interactive create a heap dumps via the jconsole, a tool which is included in the JDK. Type jconsole in the command line to start it.
To allow a Java program the access of jconsole use the -Dcom.sun.management.jmxremote start option.
Use them MBeans | com.sun.management | HotSpotDiagnostics | Operations |DumpHead .
More info can be found in JConsole and JConsole Tutorial.
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system