Getting head dump
1. Use jmap (which is also part of the Sun JDK 6) to dump the heap of a running application to a file using a command line like
jmap -heap <pid>
2. Tell the VM to make a heap dump when an OutOfMemoryException occurs by adding:
to the VM parameters at startup. With another switch you can specify the path for the dumps:
Analyzing heap dump
1. Supplying jhat the hprof-file you can point your browser to the integrated web server of jhat and browse the heap looking for the objects that take up your memory.
2. MAT – Memory Analyzer tool
Getting thread dump
Thread dump is stack traces for each thread in the JVM output to stdout as text
CTRL+BREAK if JVM is the foreground process
CTRL+C if JVM is the foreground process
Free memory on Linux
vmstat 2 5
Checking the System Load
Enable verbose GC
-verbose:gc –XX:+PrintGCDetails –XX:+PrintGCTimeStamps –Xloggc:/gc.log
The command line option
-verbose:gc causes information about the heap and garbage collection to be printed at each collection. For example, here is output from a large server application:
[GC 325407K->83000K(776768K), 0.2300771 secs]
[GC 325816K->83372K(776768K), 0.2454258 secs]
[Full GC 267628K->83769K(776768K), 1.8479984 secs]
Here we see two minor collections followed by one major collection. The numbers before and after the arrow (e.g.,
325407K->83000K from the first line) indicate the combined size of live objects before and after garbage collection, respectively. After minor collections the size includes some objects that are garbage (no longer alive) but that cannot be reclaimed. These objects are either contained in the tenured generation, or referenced from the tenured or permanent generations.
The next number in parentheses (e.g.,
(776768K) again from the first line) is the committed size of the heap: the amount of space usable for java objects without requesting more memory from the operating system. Note that this number does not include one of the survivor spaces, since only one can be used at any given time, and also does not include the permanent generation, which holds metadata used by the virtual machine.
The last item on the line (e.g.,
0.2300771 secs) indicates the time taken to perform the collection; in this case approximately a quarter of a second.
- PSYoungGen refers to the garbage collector in use for the minor collection. PS stands for Parallel Scavenge.
- The first set of numbers are the before/after sizes of the young generation and the second set are for the entire heap. (Diagnosing a Garbage Collection problem details the format)
- The name indicates the generation and collector in question, the second set are for the entire heap.
An example of an associated full GC also shows the collectors used for the old and permanent generations:
3.757: [Full GC [PSYoungGen: 2672K->0K(35584K)]
[ParOldGen: 3225K->5735K(43712K)] 5898K->5735K(79296K)
[PSPermGen: 13533K->13516K(27584K)], 0.0860402 secs]
Finally, breaking down one line of your example log output:
8109.128: [GC [PSYoungGen: 109884K->14201K(139904K)] 691015K->595332K(1119040K), 0.0454530 secs]
- 107Mb used before GC, 14Mb used after GC, max young generation size 137Mb
- 675Mb heap used before GC, 581Mb heap used after GC, 1Gb max heap size
- minor GC occurred 8109.128 seconds since the start of the JVM and took 0.04 seconds
Get JVM flag information
e.g., to check the value of -XX:PermSize JVM option run
jinfo -flag PermSize
How to get memory usage of tomcat 7 using JMX API?
Where to set java heap options (e.g. -Xmx) for Tomcat 6 under Ubuntu 9.04
The most elegant way I’ve found so far is to edit /etc/init.d/tomcat6 and add this at the top of the file:
# local config settings