Support free tutorials

vogella training Training Books

Eclipse Memory Analyzer (MAT) - Tutorial

Lars Vogel

Version 1.1


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. Memory handling in Java
1.1. Native Memory
1.2. Memory in Java
1.3. Java Heap
1.4. Java Stack
1.5. Escape analysis
1.6. Memory leaks
2. Analyzing memory leaks with Eclipse
2.1. Heap dump
2.2. Analyzing Android heap dumps with Eclipse
2.3. Use the Eclipse Memory Analyzer
3. Installation
4. Example
4.1. Create Project
4.2. Run Project
5. jconsole
6. About this website
6.1. Donate to support free tutorials
6.2. Questions and discussion
6.3. License for this tutorial and its code
7. Links and Literature
7.1. Source Code
7.2. MAT Resources
7.3. vogella Resources

1. Memory handling in Java

Java handles its memory in two areas. The heap and the stack. We will start with a short overview of memory in general on a computer. Then the Java heap and stack is explained.

1.1. Native Memory

Native memory is the memory which is available to a process, e.g. the Java process. Native memory is controlled by the operating system (OS) and based on physical memory and other physical devices, e.g. disks, flash memory, etc.

The processor (CPU) of the computer computes the instructions to execute and stores its computation results into registers. These registers are fast memory elements which stores the result of the CPU. The processor can access the normal memory over the memory bus. A amount of memory a CPU can access is based on the size of the physical address which the CPU uses to identify physical memory. A 16-bit address can access 2^16 (=65.536) memory locations. A 32-bit address can access 2^32 (=4.294.967.296) memory locations. If each memory area consists of 8 bytes then a 16-bit system can access 64KB of memory and the 32-bit system can access 4GB of memory.

An operating system (OS) normally uses virtual memory to map the physical memory to memory which each process can see. The OS assigns then memory to each process in a virtual memory space for this process and maps access to this virtual memory to the real physical memory.

Current 32-bit systems uses an extension (Physical Address Extension (PAE)) which extends the physical space to 36-bits of the operation system. This allows the OS to access 64GB. The OS uses then virtual memory to allow the individual process 4 GB of memory. Even with PAE enabled a process can not access more than 4 GB of memory.

Of course with a 64-bit OS this 4GB limitation does not exist anymore.

1.2. Memory in Java

Java manages the memory for use. New objects created and placed in the heap. Once your application have no reference anymore to an object the Java garbage collector is allowed to delete this object and remove the memory so that your application can use this memory again.

1.3. Java Heap

In the heap the Java Virtual Machine (JVM) stores all objects created by the Java application, e.g. by using the "new" operator. The Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can faster identify objects which can get removed.

The memory for new objects is allocated on the heap at run time. Instance variables live inside the object in which they are declared.

1.4. Java Stack

Stack is where the method invocations and the local variables are stored. If a method is called then its stack frame is put onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.

1.5. Escape analysis

As stated earlier Java objects are created and stored in the heap. The programming language does not offer the possibility to let the programmer decide if an object should be generated in the stack. But in certain cases it would be desirable to allocate an object on the stack, as the memory allocation on the stack is cheaper than the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the runtime.

The JVM uses therefore internally escape analysis to check if an object is used only with a thread or method. If the JVM identify this it may decide to create the object on the stack, increasing performance of the Java program.

1.6. Memory leaks

The garbage collector of the JVM releases Java objects from memory as long as no other object refers to this object. If other objects still hold references to these objects, then the garbage collector of the JVM cannot release them.

2. Analyzing memory leaks with Eclipse

2.1. Heap dump

A Java heap dump is a snapshot of the complete Java object graph at a certain point in time. It is stored in a binary format called HPROF.

It includes all objects, fields, primitive types and object references.

The Eclipse MAT tooling is a set of plug-ins which visualize the references to objects based on Java heap dumps and provides tools to identify potential memory leaks.

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.

2.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 use 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.

2.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.

3. Installation

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

4. Example

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

4.2. Run Project

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

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

5. jconsole

You can also interactively 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 start option.

Use them MBeans | | HotSpotDiagnostics | Operations |DumpHeap .

More info can be found in JConsole and JConsole Tutorial.

6. About this website

6.1. Donate to support free tutorials

Please consider a contribution Support free tutorials if this article helped you. It will help to maintain our content and our Open Source activities.

6.2. Questions and discussion

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

6.3. License for this tutorial and its code

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

7. Links and Literature

7.1. Source Code

Source Code of Examples

7.3. vogella Resources

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”. The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.