Monday, January 18, 2016

Eclipse Memory Analyzer (MAT) - Tutorial

1. Memory handling in Java


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. Using heap dumps to get a snapshot of the memory of an application

heap dump is a snapshot of the complete Java object graph on a Java application 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.

2.2. The Eclipse Memory Analyser (MAT) tooling

The Eclipse Memory Analyser Tooling (MAT) is a set of plug-ins for the Eclipse IDE which provides tools to analyze heap dumps from Java application and to identify memory problems in the application. This helps the developer to find memory leaks and high memory consumption issues.
It visualizes the references to objects based on Java heap dumps and provides tools to identify potential memory leaks.

3. 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 Android heap dump format is similar to the Java heap dump format but not exactly the same. Eclipse MAT can work directly with the Android heap dump format.

4. Installation

Install Eclipse MAT via the Help → Install New Software... menu entry. Select the update site of your release from the drop-down box and once its content is downloaded, select General Purpose Tools and its sub-entries Memory Analyzer and Memory Analyzer(Charts).
Installation of MAT

5. Creating heap dumps for Java programs

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 aOutOfMemoryError error. To instruct the JVM to create a heap dump in such a situation, start your Java application with the -XX:+HeapDumpOnOutOfMemoryError option.
Use the File → New → Other... → Other → Heap Dump menu entry to open a dialog to select for which process you want to acquire a memory dump.
Creating a heap dump
Select the process for a heap dump in the following dialog and press the Finish button.
Alternatively you can also interactively create a heap dump via Eclipse. For this open the Memory Analysis perspective via Open Perspective → Other....
Opening the MAT perspective
If you trigger the creation of the heap manually the JVM performs a garbage collector run before it writes the heap dump.

6. 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. If you used MAT to create the heap dump, it should be opened automatically.
You may need to refresh your project (F5 on the project). Double-click the file and select the Leak Suspects Report.
Opening a leak 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.
Opening the dominator tree
In the dominator tree you see the references which are hold.
MAT dominator tree
To find which element is holding the reference to this object, select the entry and select Find shortest path to GC root from the context menu.


7. Example

7.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 list = new ArrayList(); while (1<2){ list.add("OutOfMemoryError soon"); } } }

7.2. Run project and create heap dump

In Eclipse add the -XX:+HeapDumpOnOutOfMemoryError to the runtime configuration.
Run the project. It crashes and writes an heap dump.

7.3. Use MAT to analyze the heap dump

Open the heap dump in MAT and get familiar with using the MAT tooling.

No comments: