DOES JAVA HAVE MEMORY LEAKS
DOES JAVA HAVE MEMORY LEAKS
1105130516, Ed Lycklama 1
Overview
1105130516, Ed Lycklama 2
Garbage Collection in
Java™ Technology
1105130516, Ed Lycklama 3
Simple GC Example
m1.ref = m2;
m1 m2
global.ref = m2;
“global”
return;
}
now unreachable
1105130516, Ed Lycklama 4
Garbage Collection Myths
1105130516, Ed Lycklama 5
What Is a Memory Leak?
• Allocated
– Exists on the heap
• Reachable
– A path exists from some root to it
• Live
– Program may use it along some future
execution path
1105130516, Ed Lycklama 6
What Is a Memory Leak?
Memory leak in
C/C++
allocated
reachable
Memory leak
in Java
live
1105130516, Ed Lycklama 7
C++ vs. The Java
Programming Language
1105130516, Ed Lycklama 8
Nodes vs. Edges
1105130516, Ed Lycklama 9
Less Common, More Severe
1105130516, Ed Lycklama 10
Need a New Term
• Loiterer
• Object remains past its usefulness
• Distinct from memory leaks in C++
• Some of the native Java libraries have
conventional memory leaks
– Programmers using Java technology can’t
fix them
1105130516, Ed Lycklama 11
Lexicon of Loiterers
1105130516, Ed Lycklama 12
Lapsed Listener
1105130516, Ed Lycklama 13
Lapsed Listener Example
1105130516, Ed Lycklama 14
Lapsed Listener Strategies
1105130516, Ed Lycklama 15
Lingerer
1105130516, Ed Lycklama 16
Lingerer Example
1105130516, Ed Lycklama 17
Lingerer Strategies
1105130516, Ed Lycklama 18
Laggard
• Common culprits
– Changing life-cycle of class (e.g. into a singleton)
• Constructor sets up state
• Caches expensive-to-determine object
• State changes, cache not maintained
1105130516, Ed Lycklama 19
Laggard Example
1105130516, Ed Lycklama 20
Laggard Strategies
• Cache cautiously
– Only expensive, frequently used calculations
• Use a profiler to guide you
1105130516, Ed Lycklama 21
Limbo
• Common culprits
– Thread stall
– Expensive setup calls long-running analysis
1105130516, Ed Lycklama 22
Limbo Example
1105130516, Ed Lycklama 23
Limbo Strategy
1105130516, Ed Lycklama 24
Tools and Techniques
• ObjectTracker
– Lightweight instance tracking infrastructure
– Invasive: requires code modification
– Find loiterers of a particular class
• You decide which classes to track
• Won’t tell you why it loiters
– Relies on unique hashcode
• Will not work in the Java 2 virtual machine
• May not work in other VM’s
1105130516, Ed Lycklama 25
Tools and Techniques
• Memory Debugger
– No code modification required
– Monitor overall heap usage
– Understand allocation activity by class
– Pinpoint excessive object allocation
– Identify memory leaks (loiterers)
1105130516, Ed Lycklama 26
Tools and Techniques
1105130516, Ed Lycklama 27
Demo
1105130516, Ed Lycklama 28
Questions?
1105130516, Ed Lycklama 29
Wrap-Up
1105130516, Ed Lycklama 30
Wrap-Up
1105130516, Ed Lycklama 31
Contact Info
1105130516, Ed Lycklama 32
1105130516, Ed Lycklama 33