Java, Eclipse and Web programming Tutorials
Follow me on twitter About Lars Vogel

Eclipse Memory Analyser (MAT) - Tutorial

Lars Vogel

Version 0.3

05.10.2009

Revision History
Revision 0.101.10.2009Lars Vogel
Created
Revision 0.202.10.2009Lars Vogel
Example project
Revision 0.305.10.2009Lars Vogel
sub-chapters for example

Eclipse Memory Analyser (MAT)

This article describes the usage of the Eclipse Memory Analyser (MAT) to identify memory leaks.

This article is based on Eclipse 3.5.


Table of Contents

1. Eclipse Memory Analyser (MAT)
2. Installation
3. Example
3.1. Create Project
3.2. Run Project
3.3. Use MAT
4. jconsole
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. Other Resources

1. Eclipse Memory Analyser (MAT)

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

2. Installation

Install Eclipse MAT via the update manager of eclipse (please see Eclipse Update manager for details). Select "General Purpose Tools " and install "Memory Analyser (Incubation)" and "Memory Analyser (Charts)".

3. Example

3.1. Create Project

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");
		}

	}

}

			

3.2. Run Project

In Eclipse add the -XX:+HeapDumpOnOutOfMemoryError to the runtime configuration.

Run the project.

3.3. Use MAT

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.

4. jconsole

You can also interactive create a HeapDump via the jconsole. Type jconsole in the 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 .

5. Thank you

Thank you for practicing with this tutorial.

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

6. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.

I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.

7. Links and Literature

7.1. Source Code

http://www.vogella.de/code/codeeclipse.html Source Code of Examples