Eclipse Memory Analyser

Eclipse Memory Analyzer (MAT):
Eclipse Memory Analyzer helps you find memory leaks and high memory consumption issues in your application.

What is heap dump?
A heap dump is a snapshot of memory used by our application at a given point in time. It contains information on the Java objects and classes in memory at the time the snapshot was taken.

What is the use of heap dump?
If your Java application crashes due to OutOfMemoryError then we need heap dump to analyze to figure out what caused the error. This can help decide what to optimize in your code.


Steps to install and use MAT :

1) Install MAT in eclipse :
- Visit this link

Click image to Zoom





- As show in above image put this link which is in "Update Site" in your eclipse
ie. In Help > Install New Software..., enter the following URL:
http://download.eclipse.org/mat/1.5/update-site/


Click to Zoom























2) Generate a heap dump :
To generate heap dump we have to execute JVM with the appropriate parameters in eclipse.
Syntax:
-XX:+HeapDumpOnOutOfMemoryError

This writes heap dump on first  OutOfMemoryError

Put this in eclipse
-Xmx512m -XX:+HeapDumpOnOutOfMemoryError

Click to Zoom
















Click to Zoom



















- Output in console
Click to Zoom









Note : You should run the application once to view the java class in run-configuration option.

3) View generated heap dump in eclipse :
Press F5 in your project folder, so now the generated heap dump file will appear in eclipse.


















4)Load Heap Dump :
Just double click on the heap dump file and select the Leak Suspects Report, and then this file will be loaded into MAT.

Click to Zoom
















5) Analyze Heap Dump :
Clicking on the “See stacktrace” link will give you the exact line of code that was causing the application to crash.

Click to Zoom















Click to Zoom












 Thread stack gives the line no. where application crashes or consumes more memory.


Note:
If there is error then change the -Xmx size ie. from 512 to 10.

What is Xms or -Xmx1024m ?
In simple words, you are saying Java to use Maximum of 1024MB from available memory
eg.
-Xmx83886080
-Xmx81920k
-Xmx80m

Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes.

GenerateHeap.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.heapAnalyser;

import java.util.ArrayList;
import java.util.List;

public class GenerateHeap 
{
public static void main(String[] args) 
{
        System.out.println("JVM OutOfMemoryError Simulator");
        List<String> leakingVariable = new ArrayList<String>();
        
        try 
        {
                while (1 < 2) 
                {
                leakingVariable.add("OutOfMemoryError");
                }
        } 
        catch (Throwable exp) 
        {
             if (exp instanceof java.lang.OutOfMemoryError) 
             {
             System.out.println("OutOfMemoryError triggered! " + "[" + exp + "]");
             }
             else 
             {
             System.out.println("Other Exception! " + "[" + exp + "]");
             }
        }
        System.out.println("Simulator done!");
}
}

No comments:

Post a Comment