Version 0.3
Copyright © 2009 Lars Vogel
05.10.2009
| Revision History | ||
|---|---|---|
| Revision 0.1 | 01.10.2009 | Lars Vogel |
| Created | ||
| Revision 0.2 | 02.10.2009 | Lars Vogel |
| Example project | ||
| Revision 0.3 | 05.10.2009 | Lars Vogel |
| sub-chapters for example | ||
Table of Contents
The Java Garbage Collector releases Java objects from memory as long as no other object refers to this object.
A Java heap dump is an image 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 of a OutOfMemoryError.
The Eclipse MAT helps to visualize based on Java heap dumps the references to objects and provides tools to identify potential memory leaks.
To tell the JVM to create a heapdump in case of an OutOfMemoryError use the option -XX:+HeapDumpOnOutOfMemoryError
Install Eclipse MAT via the Eclipse Update manager . Select "General Purpose Tools " and install "Memory Analyser (Incubation)" and "Memory Analyser (Charts)".
Create the Java project "de.vogella.mat.first" and the package "de.vogella.mat.first". Create the following class.
package de.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"); } } }
In Eclipse add the -XX:+HeapDumpOnOutOfMemoryError to the runtime configuration.

Run the project.
You should get a new file in your project (.hprof). You may need to refresh your project (F5 on the project). Double-click it and select "Leak Suspects Report".

Switch back to the overview and start your analyses of the heap dump. I believe the user interface is quite intuitive. Expecially the dominator tree gives quickly an overview of the used objects.

You can also interactive create a HeapDump via the jconsole. 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.de Google Group. I have created a short list how to create good questions which might also help you.
Eclipse RCP Training (German) Eclipse RCP Training with Lars Vogel
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