Out Of Memory - Analyze Your Java Heap
A short introduction on why memory usage might not be what you expect it to be and tools for going deeper. Different ways for acquiring memory snapshots and how to examine them with MAT.
Roger Lindsjö, Ericsson
2. GC silver bullet
• Unreferenced memory cleared
Fragmentation cleanup
Incorrect understanding of data / structures
• Packratting
3. Data / Structures
• Primitives vs Objects
Byte vs byte
• Overhead of structure
• LinkedList vs ArrayList
JVM settings
• UseCompressedOops, UseCompressedStrings
26. Watch out
• Can be large
Takes time to produce
• Can contain sensitive information
27. LInks
MAT http://www.eclipse.org/mat/
jconsole
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jconsole.html
jmap
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jmap.html
jstat
http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html
• Java API http://www.eclipse.org/forums/index.php/t/243401/
Editor's Notes
The data we need might be larger than we realize Storing too much intentionally (Caches) Storing too much unintentionally (forgetting to dereference)
Primitive types vs objects Referencing same object vs a copy Integer.valueOf...
Generate a snapshot by sending signal, automatically from keyboard or different external programs.
Sample program which tries to store short strings by doing substring on large strings (which actually uses the same large char array as the large string.
Different aspects of using MAT
Open a snapshot
Store each snapshot in a separate folder since opening it in MAT will create several index files used by MAT and will soon clutter up a folder with several snapshots.
Opening can take some time (indexes are generated)
After startup dirrerent reports can automatically be run.
MAT shows if a few objects are holding on to most of the memory and makes them suspects.
Generate class histograms from the histogram button.
You can now see the number of objects, their shallow heap (memory by the object itself) and retained heap (memory by objects referenced, such as lists, maps etc)
To the left info about the class / instance can be seen
Show instances of this class with instances pointing to it.
Filter histogram with regexp
Show instances with what it points to
StoreLargeString is small, but references a List which is large, has an array of 1000000 elements, and 1000000 strings, each referencing an array of 26KB
Select previous searches into compare basket
Compare and you can see actual values or differences between queries
With OQL you can select objects from their type and member values
Handle the snapshots with care, they can contain sensitive information
Links. The Java API mainly points to discussion of how to use.