vrijdag 9 mei 2014

MAD about MAT! The 'simple' tutorial!

What’s MAT (Eclipse Memory Analyzer)

It help reads your java heap dumps and helps you connects the dots. Mat is an a great a heap dump analyzer, developed by Eclipse/IBM & SAP.
It's supported for the SUN jvm and the IBM jvm.


MAT ( Eclipse Memory Analyzer)


Is it easy?

Well to be honest, No! You can write a book about the jvm and how to fix memory problems. I hoping after you finished reading this you can find the big problems easy. The harder problems you probably will find after reading some more tutorials and getting some experience under your belt.

Download

MAT is free! So Please download and try it out yourself.
http://www.eclipse.org/mat/downloads.php
TIP: Download it Now while you read. What do you have to lose a few MB?

How to use?

I have searched the Internet, finding many different tutorials. Instead of writing my own, I have found the best way of working was thanks to the advice of Vatel

Hello Markus, ⇐ owner of an excellent performance blog.

I would like to share my own experience of using MAT.
MAT is really a great tool, but your should known well how to use it! Unfortunately, by default MAT shows some useless windows, so you may be disappointed.

OK, below is my "How to", it would be great to compose a tutorial based on it and include it into MAT docs. I included screenshot of my example "my.leaks" application

===============================================

How to use Eclipse MAT to analyze memory dumps.

===============================================

Initial steps:

- if working with large heap dumps (> 1 Gb), it is recommended to increase Xmx value in eclipse.ini (ex. -Xmx2400m)
- open your dump (File -> Open heap dump), wait until parsing is finished
- for a fast preview, make a summary histogram (button "Create a histogram from an arbitrary set of object" on the toolbar),
sort by retaind heap, look at object types which retain most of memory (usually, String/char[] and byte[])


1. The first way - using dominator tree grouped by packets/classes

1.1  The DOMINATOR tree (author's remark: I love this word!)

Press button "Open query browser", in the drop-down menu select "Java Basics -> Open in dominator tree",
type a filter for your classes in "objects" field, ex. "com.yourcompany.yourproject.*",
in the "groupby" field, select "by package", look at the results (retained heap), expand your packets and subpackets,
see how much memory is retained by each packet/subpacket.
This way you may understand which module(s) of your system consume most of memory.

In my example this "my.leaks.*"

Screen MAT ( Eclipse Memory Analyzer)


Selecting the dominator tree


Java Basics / Open In Dominator Tree

1.2 Show retained set

Select a packet (or a class) you want to investigate (which retains much memory),
in the drop-down menu select "Show retained set",
look at object types which consume most of memory (in the context of the selected package),
you should sort the results by retained heap.
So, you see which types of objects are consumed by some of your modules/subsystems.

Dominator tree by package
Show retained set



1.3 Show largest objects

Click on some object type (usually, you look at char[] and byte[]),
in the context menu select "List objects -> with incoming references", sort the results by retained heap.
This way, you will see largest objects of the selected type. You may see their contents, copy them into clipboard
or a file ("Copy" in the context menu)
To see more objects, click on the "Total" line (in the bottom) and select "Next" in the context menu.

MAT - List Object with incoming refrence


1.4 Analyse

Select an object instance from the result, look at its contents, and also expand incoming references,
analyze why this object is referenced and why this value is here (this depends on your business logic).
It makes sense to look at more objects from the result and analyze them.



1.5 Repeat

Repeat steps for different object types and also different packets and/or classes
(you are interested in objects which retain most of heap).



2. Another way (Using the histogram)

2.1 Histogram

On the summary histogram (see Initial Steps above) select a type (class) which retains much memory (ususally char[]/String, byte[])

Menu MAT - Select Histogram



2.2 incoming references

In the context menu select "Show object by class -> by incoming references"
Histogram MAT - Selecting incoming refrences


2.3 Expand the resulting tree

look - which class references to it, then look which class references to that class, etc.




2.4 Retained Set

At each step you may look at the retained set of any class ("Show retained set" in the context menu)
or at the concrete object instances ("List objects -> with incoming references" in the context menu).

Resource: http://kohlerm.blogspot.com/2009/07/eclipse-memory-analyzer-10-useful.html
The end

My personal favorite/alternative

Instead of selecting List Objects, choose Select Show Objects By Class ->  by incoming references"

And expand:

Show Objects By Class ->  by incoming references



The answer: Here our service CreateLeaks is creating a leak(what's in a name).

Extra information

Wait, wait, what did he say?
You can some great reference information at:

short version:

http://yourkit.com/docs/95/help/sizes.jsp

some what longer version:

http://kohlerm.blogspot.com/2009/02/how-to-really-measure-memory-usage-of.html

Some good slides: last view slide give you some usefull advice. Like how to dump in your jvm(IBM and SUN)
http://www.slideshare.net/AJohnson1/extending-eclipse-memory-analyzer


So why am I MAD about MAT/Conclusion

MAT is an excellent, and you will find lots of great tutorials. Who will explain to you a lot. I mean really a lot! (Time consuming) Personal advice follow the wise words of Vatel and try and not to loose too much time. If you want to know more... just read http://java.dzone.com/news/how-fix-memory-leaks-java