Jump to: navigation, search

Difference between revisions of "MemoryAnalyzer"

m (Tasks)
(Getting Started)
(33 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
== About ==
 
== About ==
 
The [http://eclipse.org/mat Eclipse Memory Analyzer] is a fast and feature-rich heap analyzer that helps you find '''memory leaks''' and high '''memory consumption''' issues.
 
The [http://eclipse.org/mat Eclipse Memory Analyzer] is a fast and feature-rich heap analyzer that helps you find '''memory leaks''' and high '''memory consumption''' issues.
 +
 +
A summary of the releases is available at [[MemoryAnalyzer/Releases]]
  
 
== Getting Started ==
 
== Getting Started ==
  
[http://www.eclipse.org/mat/downloads.php Download] the latest version, run the '''Memory Analyzer Basics''' Cheat Sheet (''Help -> Cheat Sheets...'').
+
View the [http://live.eclipse.org/node/520 Webinar] recorded on 29 May 2008. (It has lots of annoying sound drop-outs.)
  
A good starting point might also be this blog about [http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/ Finding a Leaking Workbench Window].
+
Note that [http://www.vogella.de/articles/EclipseMemoryAnalyser/article.html Eclipse Memory Analyzer Tutorial] is out of date and does not explain how to install MAT in the current version of Eclipse.
 +
 
 +
Run the [http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/  leak report] to automatically detect memory leaks.
 +
 
 +
Analyze Eclipse: [http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/ Finding a Leaking Workbench Window].
 +
 
 +
The [[MemoryAnalyzer/FAQ]] is under construction, but might help by problems.
 +
 
 +
== Concepts ==
 +
 
 +
===Garbage Collection Roots===
 +
 
 +
The Garbage Collector (GC) ([http://en.wikipedia.org/wiki/Garbage_Collection Wikipedia]) is responsible for removing objects that will never be accessed anymore. Objects cannot be accessed if they are not reachable through any reference chain. The starting point of this analysis are the '''Garbage Collection Roots''', i.e. objects that are assumed to be reachable by the virtual machine itself. Objects that are reachable from the GC roots remain in memory, objects that are not reachable are garbage collected.
 +
 
 +
Common GC Roots are objects on the call stack of the current thread (e.g. method parameters and local variables), the thread itself, classes loaded by the system class loader and objects kept alive due to native code.
 +
 
 +
GC Roots are very important when determining why an object is still kept in memory: The reference chain from an arbitrary object to the GC roots (''Path to GC Roots...'') tells who is accidentally keeping a reference.
  
 
== Tasks ==
 
== Tasks ==
Line 12: Line 30:
 
=== Getting a Heap Dump ===
 
=== Getting a Heap Dump ===
  
The Memory Analyzer works with heap dumps, more specifically ''HPROF binary formatted heap dumps''. Those heap dumps are written by Sun HotSpot and any VM derived from HotSpot. Depending on your scenario, your OS platform and your JDK version, you have different options to acquire a heap dump.
+
==== HPROF dumps from Sun Virtual Machines ====
 +
 
 +
The Memory Analyzer can work with ''HPROF binary formatted heap dumps''. Those heap dumps are written by Sun HotSpot and any VM derived from HotSpot. Depending on your scenario, your OS platform and your JDK version, you have different options to acquire a heap dump.
  
 
'''Non-interactive'''
 
'''Non-interactive'''
Line 22: Line 42:
 
As a developer, you want to trigger a heap dump on demand. On '''Windows, use JDK 6 and JConsole'''. On '''Linux and Mac OS X''', you can also use '''jmap''' that comes with JDK 5.
 
As a developer, you want to trigger a heap dump on demand. On '''Windows, use JDK 6 and JConsole'''. On '''Linux and Mac OS X''', you can also use '''jmap''' that comes with JDK 5.
  
 +
Via MAT:
 +
* tutorial [http://www.bonitasoft.org/blog/eclipse/acquire-heap-dump-from-mat/ here]
  
Via Java VM parameters
+
Via Java VM parameters:
  
 
* -XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)
 
* -XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)
Line 31: Line 53:
 
Via Tools:
 
Via Tools:
  
* Sun JMap: jmap.exe -dump:format=b,file=HeapDump.hprof <pid>
+
* Sun (Linux, Solaris; not on Windows) [http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jmap.html JMap Java 5]: '''jmap -heap:format=b <pid>'''
 +
* Sun (Linux, Solaris; Windows see link) [http://java.sun.com/javase/6/docs/technotes/tools/share/jmap.html JMap Java 6]: '''jmap.exe -dump:format=b,file=HeapDump.hprof <pid>'''
 +
* Sun (Linus, Solaris) JMap with Core Dump File: '''jmap -dump:format=b,file=HeapDump.hprof /path/to/bin/java core_dump_file'''
 
* Sun JConsole: Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean
 
* Sun JConsole: Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean
 
* SAP JVMMon: Launch jvmmon.exe and call menu for dumping the heap
 
* SAP JVMMon: Launch jvmmon.exe and call menu for dumping the heap
Line 69: Line 93:
 
| 1.5.0_07
 
| 1.5.0_07
 
| Yes
 
| Yes
|  
+
| Yes (Since 1.5.0_15)
 
| Yes
 
| Yes
 
| Yes (Only Solaris and Linux)
 
| Yes (Only Solaris and Linux)
Line 96: Line 120:
 
|}
 
|}
  
==== Do you Support IBM System Dumps? ====
+
==== System Dumps and Heap Dumps from IBM Virtual Machines ====
  
Sorry, currently only HPROF formatted heap dumps are supported. However, we are working together with IBM to support IBM system dumps based on [http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/topic/com.ibm.java.doc.diagnostics.60/html/contents.html#ToC_458 DTFJ]. We expect a solution around Q4 2008.
+
Memory Analyzer can also read memory-related information from IBM system dumps and from Portable Heap Dump (PHD) files. For this purpose one just has to install the IBM DTFJ feature into Memory Analyzer version 0.8 or later. Follow the [http://www.ibm.com/developerworks/java/jdk/tools/dtfj.html IBM DTFJ feature installation instructions]. If the DTFJ feature is successfully installed then 'File' 'Open Heap Dump' should give the following options for the file types:
 +
 
 +
* All known formats
 +
* HPROF binary heap dumps
 +
* IBM Javacore file
 +
* IBM Portable Heap Dump
 +
* IBM Portable Heap Dump (gzipped)
 +
* IBM SDK for Java (J9) JExtract output XML
 +
* IBM SDK for Java (J9) JExtracted system dump
 +
* IBM SDK for Java (J9) system dump
 +
* IBM SDK for Java (J9) JExtracted system dump
 +
* IBM SDK for Java 1.4.2 JExtracted system dump
 +
 
 +
 
 +
Minimum-required versions of IBM Virtual Machines to generate the dump are:
 +
 
 +
'''IBM JDK 1.4.2 SR12, 5.0 SR8a and 6.0 SR2'''
 +
 
 +
though previous versions may generate dumps usable with Memory Analyzer but with inaccurate root information.
 +
 
 +
The [http://www.ibm.com/developerworks/java/jdk/diagnosis/ IBM Diagnostics Documentation] contains details about how to trigger a system dump.
 +
 
 +
=====IBM Java 5.0 and Java 6 Virtual Machine dump options=====
 +
 
 +
A quick reference for IBM Java 5.0 and Java 6 Virtual Machine command line options:
 +
* -Xdump:system+heap+java:events=systhrow+user,filter=java/lang/OutOfMemoryError,request=exclusive+prepwalk+compact
 +
 
 +
Dump types:
 +
* system - a system process dump. Process system dump files with jextract before loading them into Memory Analyzer. Do not give them a .sdff extension as that is only used for Java 1.4.2 system dumps
 +
* heap - a Portable Heap Dump (PHD) file. Contains all objects and classes, but no thread details
 +
* javacore - a readable file which contains information about class loaders and which can be used by Memory Analyzer when reading PHD files
 +
 
 +
Events:
 +
* systhrow - when a system generated exception is thrown
 +
* user - the user has typed control-break
 +
 
 +
Filter:
 +
* java/lang/OutOfMemoryError - the type of the systhrow exception
 +
 
 +
Request:
 +
* exclusive Stop anything modifying the heap while generating the dump
 +
* prepwalk Make sure the heap is safe to dump
 +
* compact minimise the size of the heap
 +
 
 +
=====IBM Java 1.4.2 dump options=====
 +
 
 +
A quick reference for IBM Java 1.4.2 dump options
 +
 
 +
* export JAVA_DUMP_OPTS=ONOUTOFMEMORY(SYSDUMP,HEAPDUMP,JAVADUMP)
 +
* set JAVA_DUMP_OPTS=ONOUTOFMEMORY(SYSDUMP,HEAPDUMP,JAVADUMP)
 +
 
 +
On non-z/OS systems process any system dump files with JExtract to give a .sdff file. On z/OS systems copy the SVC dump file in binary mode to your Eclipse Memory Analyzer system, giving it a .dmp file extension.
 +
 
 +
=====IBM Support Assistant=====
 +
 
 +
The latest version 4.1 of [http://www-01.ibm.com/software/support/isa/ IBM Support Assistant] has a technical preview of [http://www-01.ibm.com/support/docview.wss?uid=swg27013116#IBM%20Monitoring%20and%20Diagnostic%20Too IBM Monitoring and Diagnostic Tools for Java - Memory Analyzer] which contains version 0.8 of Eclipse Memory Analyzer together with the latest IBM DTFJ feature packaged for IBM Support Assistant.
 +
 
 +
=====Previous versions of Memory Analyzer=====
 +
 
 +
The previous version of Memory Analyzer, version 0.7, required both a DTFJ Adapter feature and a DTFJ feature. It is strongly recommended that you upgrade to Memory Analyzer version 0.8. If for some reason you cannot do this then follow the
 +
[http://www.ibm.com/developerworks/java/jdk/tools/mat.html installation instructions for the old IBM DTFJ Adapter feature and back level IBM DTFJ feature]. If you have DTFJ problems then there is an update for just the DTFJ feature. Follow the [http://www.ibm.com/developerworks/java/jdk/tools/dtfj.html IBM DTFJ feature installation instructions] for the latest level of DTFJ.
  
 
==== What if the Heap Dump is NOT Written on OutOfMemoryError? ====
 
==== What if the Heap Dump is NOT Written on OutOfMemoryError? ====
Line 119: Line 203:
  
 
=== Finding Memory Leaks ===
 
=== Finding Memory Leaks ===
 +
 +
Start by running the [http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/ leak report] to automatically check for memory leaks.
 +
 +
This blog details [http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/ How to Find a Leaking Workbench Window].
  
 
The Memory Analyzer grew up at SAP. Back then, Krum blogged about  
 
The Memory Analyzer grew up at SAP. Back then, Krum blogged about  
 
[https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/6856 Finding Memory Leaks with SAP Memory Analyzer]. The content is still relevant!
 
[https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/6856 Finding Memory Leaks with SAP Memory Analyzer]. The content is still relevant!
 
This blog details [http://dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/ how to find a leaking Workbench Window].
 
  
 
=== Analyzing Java Collection Usage ===
 
=== Analyzing Java Collection Usage ===
Line 129: Line 215:
 
Check out Krum's blog about [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7680 Analyzing Java Collections Usage with Memory Analyzer]. Also, [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7006 Memory for Nothing] looks unused collections and the memory kept alive.
 
Check out Krum's blog about [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7680 Analyzing Java Collections Usage with Memory Analyzer]. Also, [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7006 Memory for Nothing] looks unused collections and the memory kept alive.
  
=== Perm Space Issus ===
+
=== Perm Space Issues ===
  
Vedran has blogged some hints how to address [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7636 Perm Space Issues].
+
A good starting point is the blog [http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/17/the-unknown-generation-perm/ The Unknown Generation: Perm] by Andreas.
 +
 
 +
Also, Vedran has blogged some hints how to address [https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/7636 Perm Space Issues].
  
 
=== Instance Segmentation ===
 
=== Instance Segmentation ===
  
 
Often it is possible to spot problem by segmenting objects along different attributes. Elena has blogged about [http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/08/the-power-of-aggregation-making-sense-of-the-objects-in-a-heap-dump/ The Power of Aggregation: Making sense of the Objects in a Heap Dump].
 
Often it is possible to spot problem by segmenting objects along different attributes. Elena has blogged about [http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/08/the-power-of-aggregation-making-sense-of-the-objects-in-a-heap-dump/ The Power of Aggregation: Making sense of the Objects in a Heap Dump].
 +
 +
=== Detect duplicated/conflicting libs/classes ===
 +
 +
Tutorial: [http://www.bonitasoft.org/blog/eclipse/an-effective-way-to-fight-duplicated-libs-and-version-conflicting-classes-using-memory-analyzer-tool/ an efficient way to Detect duplicated/conflicting libs/classes ]

Revision as of 11:01, 11 June 2012

About

The Eclipse Memory Analyzer is a fast and feature-rich heap analyzer that helps you find memory leaks and high memory consumption issues.

A summary of the releases is available at MemoryAnalyzer/Releases

Getting Started

View the Webinar recorded on 29 May 2008. (It has lots of annoying sound drop-outs.)

Note that Eclipse Memory Analyzer Tutorial is out of date and does not explain how to install MAT in the current version of Eclipse.

Run the leak report to automatically detect memory leaks.

Analyze Eclipse: Finding a Leaking Workbench Window.

The MemoryAnalyzer/FAQ is under construction, but might help by problems.

Concepts

Garbage Collection Roots

The Garbage Collector (GC) (Wikipedia) is responsible for removing objects that will never be accessed anymore. Objects cannot be accessed if they are not reachable through any reference chain. The starting point of this analysis are the Garbage Collection Roots, i.e. objects that are assumed to be reachable by the virtual machine itself. Objects that are reachable from the GC roots remain in memory, objects that are not reachable are garbage collected.

Common GC Roots are objects on the call stack of the current thread (e.g. method parameters and local variables), the thread itself, classes loaded by the system class loader and objects kept alive due to native code.

GC Roots are very important when determining why an object is still kept in memory: The reference chain from an arbitrary object to the GC roots (Path to GC Roots...) tells who is accidentally keeping a reference.

Tasks

Getting a Heap Dump

HPROF dumps from Sun Virtual Machines

The Memory Analyzer can work with HPROF binary formatted heap dumps. Those heap dumps are written by Sun HotSpot and any VM derived from HotSpot. Depending on your scenario, your OS platform and your JDK version, you have different options to acquire a heap dump.

Non-interactive

If you run your application with the VM flag -XX:+HeapDumpOnOutOfMemoryError a heap dump is written on the first Out Of Memory Error. There is no overhead involved unless a OOM actually occurs. This flag is a must for production systems as it is often the only way to further analyze the problem.

Interactive

As a developer, you want to trigger a heap dump on demand. On Windows, use JDK 6 and JConsole. On Linux and Mac OS X, you can also use jmap that comes with JDK 5.

Via MAT:

Via Java VM parameters:

  • -XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)
  • -XX:+HeapDumpOnCtrlBreak writes heap dump together with thread dump on CTRL+BREAK
  • -agentlib:hprof=heap=dump,format=b combines the above two settings (old way; not recommended as the VM frequently dies after CTRL+BREAK with strange errors)

Via Tools:

  • Sun (Linux, Solaris; not on Windows) JMap Java 5: jmap -heap:format=b <pid>
  • Sun (Linux, Solaris; Windows see link) JMap Java 6: jmap.exe -dump:format=b,file=HeapDump.hprof <pid>
  • Sun (Linus, Solaris) JMap with Core Dump File: jmap -dump:format=b,file=HeapDump.hprof /path/to/bin/java core_dump_file
  • Sun JConsole: Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean
  • SAP JVMMon: Launch jvmmon.exe and call menu for dumping the heap

Heap dump will be written to the working directory.

Vendor / Release VM Parameter VM Tools
On OoM On Ctrl+Break Agent JMap JConsole
Sun, HP
1.4.2_12 Yes Yes Yes
1.5.0_07 Yes Yes (Since 1.5.0_15) Yes Yes (Only Solaris and Linux)
1.6.0_00 Yes Yes Yes Yes
SAP
1.5.0_07 Yes Yes Yes Yes (Only Solaris and Linux)

System Dumps and Heap Dumps from IBM Virtual Machines

Memory Analyzer can also read memory-related information from IBM system dumps and from Portable Heap Dump (PHD) files. For this purpose one just has to install the IBM DTFJ feature into Memory Analyzer version 0.8 or later. Follow the IBM DTFJ feature installation instructions. If the DTFJ feature is successfully installed then 'File' 'Open Heap Dump' should give the following options for the file types:

  • All known formats
  • HPROF binary heap dumps
  • IBM Javacore file
  • IBM Portable Heap Dump
  • IBM Portable Heap Dump (gzipped)
  • IBM SDK for Java (J9) JExtract output XML
  • IBM SDK for Java (J9) JExtracted system dump
  • IBM SDK for Java (J9) system dump
  • IBM SDK for Java (J9) JExtracted system dump
  • IBM SDK for Java 1.4.2 JExtracted system dump


Minimum-required versions of IBM Virtual Machines to generate the dump are:

IBM JDK 1.4.2 SR12, 5.0 SR8a and 6.0 SR2

though previous versions may generate dumps usable with Memory Analyzer but with inaccurate root information.

The IBM Diagnostics Documentation contains details about how to trigger a system dump.

IBM Java 5.0 and Java 6 Virtual Machine dump options

A quick reference for IBM Java 5.0 and Java 6 Virtual Machine command line options:

  • -Xdump:system+heap+java:events=systhrow+user,filter=java/lang/OutOfMemoryError,request=exclusive+prepwalk+compact

Dump types:

  • system - a system process dump. Process system dump files with jextract before loading them into Memory Analyzer. Do not give them a .sdff extension as that is only used for Java 1.4.2 system dumps
  • heap - a Portable Heap Dump (PHD) file. Contains all objects and classes, but no thread details
  • javacore - a readable file which contains information about class loaders and which can be used by Memory Analyzer when reading PHD files

Events:

  • systhrow - when a system generated exception is thrown
  • user - the user has typed control-break

Filter:

  • java/lang/OutOfMemoryError - the type of the systhrow exception

Request:

  • exclusive Stop anything modifying the heap while generating the dump
  • prepwalk Make sure the heap is safe to dump
  • compact minimise the size of the heap
IBM Java 1.4.2 dump options

A quick reference for IBM Java 1.4.2 dump options

  • export JAVA_DUMP_OPTS=ONOUTOFMEMORY(SYSDUMP,HEAPDUMP,JAVADUMP)
  • set JAVA_DUMP_OPTS=ONOUTOFMEMORY(SYSDUMP,HEAPDUMP,JAVADUMP)

On non-z/OS systems process any system dump files with JExtract to give a .sdff file. On z/OS systems copy the SVC dump file in binary mode to your Eclipse Memory Analyzer system, giving it a .dmp file extension.

IBM Support Assistant

The latest version 4.1 of IBM Support Assistant has a technical preview of IBM Monitoring and Diagnostic Tools for Java - Memory Analyzer which contains version 0.8 of Eclipse Memory Analyzer together with the latest IBM DTFJ feature packaged for IBM Support Assistant.

Previous versions of Memory Analyzer

The previous version of Memory Analyzer, version 0.7, required both a DTFJ Adapter feature and a DTFJ feature. It is strongly recommended that you upgrade to Memory Analyzer version 0.8. If for some reason you cannot do this then follow the installation instructions for the old IBM DTFJ Adapter feature and back level IBM DTFJ feature. If you have DTFJ problems then there is an update for just the DTFJ feature. Follow the IBM DTFJ feature installation instructions for the latest level of DTFJ.

What if the Heap Dump is NOT Written on OutOfMemoryError?

Heap dumps are not written on OutOfMemoryError for the following reasons:

  • Application creates and throws OutOfMemoryError on its own
  • Another resource like threads per process is exhausted
  • C heap is exhausted

As for the C heap, the best way to see that you won't get a heap dump is if it happens in C code (eArray.cpp in the example below):

   # An unexpected error has been detected by SAP Java Virtual Machine:
   # java.lang.OutOfMemoryError: requested 2048000 bytes for eArray.cpp:80: GrET*. Out of swap space or heap resource limit exceeded (check with limits or ulimit)?
   # Internal Error (\\...\hotspot\src\share\vm\memory\allocation.inline.hpp, 26), pid=6000, tid=468

C heap problems may arise for different reasons, e.g. out of swap space situations, process limits exhaustion or just address space limitations, e.g. heavy fragmentation or just the depletion of it on machines with limited address space like 32 bit machines. The hs_err-file will help you with more information on this type of error. Java heap dumps wouldn't be of any help, anyways.

Also please note that a heap dump is written only on the first OutOfMemoryError. If the application chooses to catch it and continues to run, the next OutOfMemoryError will never cause a heap dump to be written!

Finding Memory Leaks

Start by running the leak report to automatically check for memory leaks.

This blog details How to Find a Leaking Workbench Window.

The Memory Analyzer grew up at SAP. Back then, Krum blogged about Finding Memory Leaks with SAP Memory Analyzer. The content is still relevant!

Analyzing Java Collection Usage

Check out Krum's blog about Analyzing Java Collections Usage with Memory Analyzer. Also, Memory for Nothing looks unused collections and the memory kept alive.

Perm Space Issues

A good starting point is the blog The Unknown Generation: Perm by Andreas.

Also, Vedran has blogged some hints how to address Perm Space Issues.

Instance Segmentation

Often it is possible to spot problem by segmenting objects along different attributes. Elena has blogged about The Power of Aggregation: Making sense of the Objects in a Heap Dump.

Detect duplicated/conflicting libs/classes

Tutorial: an efficient way to Detect duplicated/conflicting libs/classes