Back to top

vogella training Training Books

Eclipse Memory Analyzer (MAT) - Tutorial

Lars Vogel

Version 1.1

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

Eclipse Memory Analyzer (MAT)

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

This article is based on Eclipse 4.3.


Table of Contents

1. Eclipse Memory Analyzer (MAT)
1.1. Analyzing heap dumps with Eclipse
1.2. Analyzing Android heap dumps with Eclipse
1.3. Use the Eclipse Memory Analyzer
2. Installation
3. Example
3.1. Create Project
3.2. Run Project
4. jconsole
5. Thank you
6. Questions and Discussion
7. Links and Literature
7.1. Source Code
7.2. MAT Resources
7.3. vogella Resources

1. Eclipse Memory Analyzer (MAT)

1.1. Analyzing heap dumps with Eclipse

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.

1.2. Analyzing Android heap dumps with Eclipse

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 Dump HPROF file 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.

1.3. Use the Eclipse Memory Analyzer

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.

2. Installation

Install Eclipse MAT via the Eclipse Update manager Select General Purpose Tools and install the Memory Analyzer and Memory Analyzer (Charts) .

3. Example

3.1. Create Project

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

3.2. Run Project

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

Run the project. It crashes and write an heap dump.

4. jconsole

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.

5. Thank you

Please help me to support this article:

Flattr this

6. Questions and Discussion

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.

7. Links and Literature

7.1. Source Code

Source Code of Examples

7.3. vogella Resources

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