09J1 template, PPT2003/Windows

Practical Lessons in
Memory Analysis
Krum Tsvetkov
SAP AG
Andrew Johnson
IBM United Kingdom Limited
GOAL
>
Learn practical tips and tricks for the analysis of
common memory-related problems
2
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
3
Why Care about Memory?
>
>
Memory is a crucial resource
Excessive memory usage can cause:



>
Crashes in the application
Unresponsive applications
Unpredictable program behavior
Memory analysis is complicated
4
How Will This Session Help Me?
>
>
>
>
Present common memory-related issues
Give hints how to solve them using heap dumps
and Memory Analyzer
Show many demos using real-life heap dumps
Show how to simplify analysis by automating it
5
Heap Dumps Are Useful for Analysis
>
>
>
>
Heap dumps come “for free”
They are suitable for production
The comprehensive data allows detailed analysis
There is a wide platform coverage


HPROF dumps from HotSpot based JVMs
DTFJ and PHD dumps from IBM JVMs
6
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
7
First, Collect Data For Analysis
>
>
>
Enable heap dumps on OutOfMemoryError
Trigger heap dumps on demand if needed
How-to for getting a heap dump:
http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_a_Heap_Dump
8
How to Get a “Good” Heap Dump?
Max
Heap
Min
Heap
>
>
Processing
Space
Base
Memory
When memory is exhausted the leak will occupy
most of the processing space
Ensure big enough processing space, this will
make the leak easier to find
9
How to Analyze Memory Leaks
>
>
>
Find the biggest objects
Analyze why they are kept in memory
Analyze what makes them big
10
DEMO
>
Analysis of a Memory Leak
11
How the Leak Analysis Was Done
>
>
>
>
>
Got a “good” heap dump
Found the biggest objects (in the dominator tree)
Analyzed who kept them alive (using paths)
Analyzed what made them big (looking at their
retained set)
Used “Leak Report” to automate the analysis
12
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
13
What Is a Heavy Request?
>
>
>
An expensive operation which can cause an
OutOfMemoryError
“Acute” rather than a “chronic” problem
Examples:


Reading a big file in memory not in chunks
Try to load a whole DB table in memory
14
Inspect the Thread to Find Its Activity
>
>
>
Look at the thread attributes - name, class, etc...
Look at the local variables
Look at the stack of the thread
15
DEMO
>
Analysis of a Heavy Request
16
How Thread Activity Was Analyzed
>
>
Inspected thread attributes and local variables (in
object explorer)
Analyzed stack trace
17
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
18
Why is Memory Footprint Important?
Max
Heap
Processing
Space
Base
Memory
Min
Heap
>
A lower memory footprint can improve:


The scaling of an application / server
Performance by increasing the time between GCs
19
Find Where Footprint Can Be Optimized
>
>
>
Inspect the set of retained objects
Search for inefficiently used data structures
Look for redundant data
20
DEMO
>
Analysis of High Memory Footprint
21
How Memory Footprint Was Analyzed
>
>
>
>
Analyzed retained objects (in dominator tree,
retained set)
Used “Group by Value” to find redundant objects
Used the commands from the “Collections” group
Used “Component Report” to automate the
analysis
22
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
23
Non-heap Memory Can Be Exhausted
>
What is in the non-heap memory?


>
Metadata for classes
Interned Strings (for some JVMs)
How can it be exhausted?



Too many interned Strings
Classes packaged and loaded multiple times
Class loaders which are not properly released
24
Why Is a Class Loader Not Released?
>
>
>
The trivial case – there is a “normal” reference to it
A common case – it is the context class loader of
a thread
The registry problem – one instance is enough
25
How to Analyze “Leaking” Class Loaders
>
>
>
Find classes loaded more than once
Find redundant loaders by their name
Look at the paths to the suspect class loaders
26
DEMO
>
Analyzing “Leaking” Class Loaders
27
How Was the “Leaking” Class Loaders
Analysis Done
>
>
>
Used the “Duplicate Classes” query
Found the loaders that load redundant classes
Analyzed why they are not released (using paths)
28
Agenda
>
>
>
>
>
>
>
Introduction
Analyzing Memory Leaks
Analyzing a Heavy Request
Reducing Memory Footprint
Non-heap Memory
Summary
Q&A
29
Summary
>
>
>
Heap dumps are very helpful for memory analysis
Memory Analyzer offers rich analysis feature set
The analysis can often be automated
30
Questions and Answers
>
Memory Analyzer Homepage @ Eclipse

http://www.eclipse.org/mat/
31
Krum Tsvetkov
[email protected]
Andrew Johnson
[email protected]
http://www.eclipse.org/mat
32