08
What is a memory leak ?
A « memory leak » (cf. http://en.wikipedia.org/wiki/Memory_leak) is a situation when program is unable to release memory it has acquired. It happens when objects are hard referenced by other objects so that JVM can’t release them.
Example
public class MemoryLab {
//a collection (a business cache for example)
static HashSet<Integer> aCache = new HashSet<Integer>();
public static [...]
05
1/ JVM instrumentation(add JVM option)
-Xdump:java+heap+system+snap:events=user,request=exclusive+prepwalk
2/ Find java process (PID)
ps aux | grep java
or
ps auxwww | grep java
or
jps -v | grep -iv jps
3/ Trigger heap dump in hprof format
jmap -dump:format=b,file=dump.hprof <PID>
4/ Analyze dump with MAT
(cf. download Eclipse Memory Analyzer http://www.eclipse.org/mat/downloads.php)
File->Open Heap Dump …
5/ Analyze dump with jhat
>launch jhat on heap dump:
jhat dump.hprof
>go to http://localhost:7000/