Download - Hp java heap dump analysis Workshop
© Copyright 2013 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Java Heap Dump AnalysisMadhavan Marimuthu / August 5, 2014
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.2 © Copyright 2013 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
Goals of this presentation
• JVM Memory Model
• Three Main Area of Heap
• Creating Heap Dump & Options
• Useful Heap Dump Analyzing Tools
• IBM Heap Analyzer & Other Tools Demo
• Analysis Java Heap Leak Patterns
• Typical Cause of Memory leaks
• Java Coding Practice Overview
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.3
JVM Memory Model
Thread & StackMethod invocations and the local variables are stored
State of the method including which line of code is executing
and the values of all local variables
Threads have their own call stack
Heap Snapshot of the complete Java object graph at a certain point in
time
Includes all objects, fields, primitive types and object references
Non-Heap Permanent Generation
JVM 6.0
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.4
JVM 6.0 is made up of
JVM Memory Model
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.5
Three Main Areas Impacting
Performance
Young Generation Short lived and smaller footprints
Newly Created objects
Broken down into three areas as Eden and Survivor spaces
Old Generation GC runs less frequently
Where older objects are maintained Larger Footprints
Permanent Generation Meta data about the objects in the heap
Information about classes and methods
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.6
Three Main Areas Impacting
Performance
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.7
OutOfMemoryError
In the heap,if cannot reclaim enough memory for a new object.
• Exception in thread "main": java.lang.OutOfMemoryError: Java heap space
• Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit
In PermGen, If there is not enough memory in the method area for creating a
new class
• Exception in thread "main": java.lang.OutOfMemoryError: PermGen space
In thread exclusive memory areas
• Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
• Exception in thread "main": java.lang.OutOfMemoryError: <reason> <stacktrace> (Native method)
In JVM stack, too many frames on the stack
Exception in thread "main": java.lang.OutOfMemoryError: request <size> bytes for <reason>. Out of
swap space?
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.8
Memory Leak - Overview
• Objects, which can be reached via a GC root reference,
but are actually no longer used by application code.
• Too many or too large objects. Ex: Cache like structures.
• Too many temporary objects. So there is just for a short
time not enough memory. Ex: Load Scenario
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.9
Creating Heap dump
Manual kill –QUIT or kill -3
JVM parameters -XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=/directory/subdirectory
Used for production system, creates heap dump when
OutOfMemory is thrown
jmap -dump:live,format=b,file=heap.dump <pid> Part of JDK
Used to create heap dump on demand
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.10
Creating Heap dump
jmap -dump:live,format=b,file=heap.dump <pid>
Part of JDK
Used to create heap dump on demand
JVM parameters
-XX:+HeapDumpOnOutOfMemoryError -
XX:HeapDumpPath=/directory/subdirectory
Produces file java_pid<pid>.hprof
Used for production system, creates heap dump when
OutOfMemory is thrown
Useful Analyzing Tools
IBM HeapAnalyzer
IBM Pattern Modeling and Analysis Tool for Java Garbage Collector
IBM Thread and Monitor Dump Analyzer for Java
Useful Analyzing Tools
• IBM HeapAnalyzer
• IBM Thread and Monitor Dump Analyzer for Java
• Memory Analyze Tool
• VisualVM
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.11
IBM Heap Analyzer Setup
What do you need ?
• Need Standard Edition version 6 or higher Java Runtime Environment (JRE)
to run this tool
Usage <Java path>java -Xmx[heapsize] -jar ha<HeapAnalyzer version>.jar
heapdump file>
For example:
/usr/java60/bin/java -Xmx1000m -jar ha146.jar heapdump1234.txt
• 64-bit Java Runtime Environment for large Java heap dump, if more than
4GB memory is needed
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.12
Related Vocabulary
Root object :An object for which no (different) object holds a reference
Parent object :An object (for example, A) that holds at least one reference to
some(different) object (for example, B). In this case, A is said to be the parent of B
Owner object: If an object has more than one parent object, a parent object is chosen
as owner object
Child object :An object (for example, B) for which at least one (different) object(for
example, A) holds a reference. In this case B is said to be the child of A
Type: Collection of same objects
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.13
Related Vocabulary
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.14
Related Vocabulary ( Cont..)
Size: The size of an object is the amount of memory that is required to hold that
object in memory
Total Size : The sub tree size of an object is the sum of its size and the sizes of all
the objects that it reached from its children
Total size Drop: a parent and its child, it’s called a total size drop.
Total size is calculated only with owner objects.
Leak Size: Total size of leak suspects. If the leak suspects contains another leak
suspect, leak size is subtracted by nested leak suspect's total size
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.15
Related Vocabulary ( Cont..)
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.16Copyright © 2013 HP Autonomy. All rights reserved. Other trademarks are registered trademarks and the properties of their respective owners.
Analysis Java Heap Leak Patterns
1. Horizontal memory leak: Many objects
are referenced from same objects
2. Vertical memory leak: Many objects
are linked together
3. Diagonal memory leak: Combination
of horizontal and vertical memory leak.
It's often found in tree structures
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.17
IBM HeapAnalyzer-Demo
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.18
IBM HeapAnalyzer Demo -Topics
· Parent and Children View from Table View and Tree View
· Address search
· Root List View
· Same Type View
· Calculates size of each objects
· Calculates total size of each subtree
· Finds total size drop in a subtree
· Shows gaps between objects by size
· Shows objects by size
· Shows objects by total size
· Shows objects by number of child
· Shows types by size
· Shows gap distribution chart
· Shows detailed information of an object
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.19
IBM Thread and Monitor Dump
Analyzer - Demo
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.20
IBM TMDA Demo -Topics
• Thread detail view
• Monitor detail view
• List of hang suspects
• Thread comparison view
• Thread comparison summary
• Java Monitor lock comparison view
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.21
Memory Analyze Tool- Demo
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.22
MAT Demo -Topics
• Running Leak Suspect Report
• List the Biggest Objects
• Finding Responsible Objects
• Querying Heap Objects (OQL)
• Analyze Class Loader
• Analyzing Threads
• Analyzing Java Collection Usage
• Analyzing Finalizer
• Comparing Objects
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.23
VisualVM- Profiling
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.24
Useful Analyzing Tools
IBM HeapAnalyzer
IBM Pattern Modeling and Analysis Tool for Java Garbage Collector
IBM Thread and Monitor Dump Analyzer for Java
24
Profiling
Heap profiling
When: GC runs frequently, large Java heap
Look for: very large allocated objects, small objects allocated at a
high rate
Output: throughput/responsiveness issues, memory allocation
patterns, memory strategies (alternative APIs, caching solutions)
CPU profiling
When: large amount of CPU time
Look for: methods with a high self-time
Output: throughput issues, identify algorithm and designs issues
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.25
Memory leaks: reference to allocated object remains unintentionally
reachable and cannot be GC'ed.
When: Java heap grow over time w/o bound, OOM errors
Look for: abnormal memory consumption,
Output: code errors
Profiling (Cont.)
• Lock contention: large number of context switches, related to CPU
utilization
• When: manual use of threads
• Look for: high number of context switches, many threads in wait
state
• Output: code errors. Tools for profiling
• Jprofiler
• jmap, jhat
• VisualVM
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.26
Typical Cause of Memory leaks• Programming errors, bugs
• Unchecked collections: Array,list,hashmap growth
• Not closing JDBC prepared statements
• Not closing scokets,File Handles
• Thread Leaks,handle leaks
• Class Loader Leaks
• Resource allocated outside of JVM
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.27
Collections exist in large numbers in many Java applications
Java Collections
Collection Default Capacity Empty Size 10k Overhead Expansion
HashSet 16 144 360k x2
HashMap 16 128 360k x2
Hash Table 11 104 360k X2+1
Linked List 1 48 240k +1
ArrayList 10 88 40k X1.5
String Buffer 16 72 24 x2
■ Collections that contain empty space introduce additional overhead
■ Default collection size may not be appropriate for the amount of data
being held
■ Example: IBM WebSphere Application Server running
PlantsByWebSphere
–When running a 5 user test load, and using 206MB of Java heap:
– 16% of the Java heap used just for the collection objects !!
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.28
Collections Memory foot print Order
Java Collections(Cont..)
Each Java Collection has a different level of function, and memory
overhead
Using the wrong type of collection can incur significant additional
memory overhead
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.29
• Heap and lock contention issues:
• Avoid create objects where primitives could be used (autoboxing is not
for free)
• Avoid String concatenations, use StringBuilder with append()
• Avoid StringBuilder overallocation, set initial size to reduce resizing
• Avoid use of synchronized keyword, look for a java.util.concurrent
solution
• Use java.util.concurrent API instead of "synchronized" Collections
• Don't use exceptions for flow control (Exception objects allocation), use
flow control constructs (if/else, return, switch).
• Don't generate stack traces, they're expensive operations.
• Reduce number of system calls:
• Avoid accessing disk: use large buffers or direct buffering
• Avoid accessing to underlying OS
• Avoid processing bytes and characters individually
• Never implement finalize(): A big pain for GC
Java Coding Practice High-Level
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.
Thank you
© Copyright 2014 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. HP Confidential.
Download References
Visual VM 1.3.6 for Java6
https://java.net/projects/visualvm/downloads/download/release136/visualvm_136.zip
IBM Heap Analyzer
ftp://public.dhe.ibm.com/software/websphere/appserv/support/tools/HeapAnalyzer/ha4
55.jar
IBM Thread and Monitor Dump Analyzer
ftp://public.dhe.ibm.com/software/websphere/appserv/support/tools/jca/jca455.jar
Memory Analyzer Tool
http://www.eclipse.org/downloads/download.php?file=/mat/1.4/rcp/MemoryAnalyzer-
1.4.0.20140604-win32.win32.x86.zip