Java Profiling

Getting head dump
2 ways:
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 -dump:format=b,file=myheap.hprof
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
jstack {pid}

CTRL+C if JVM is the foreground process
kill -3

Free memory on Linux
vmstat -s
vmstat 2 5
free -m

Checking the System Load
top -c

Enable verbose GC

-verbose:gc –XX:+PrintGCDetails –XX:+PrintGCTimeStamps –Xloggc:/gc.log

127.720: [Full GC    20260K->13975K(129792K),       0.5393601 secs]
Time      Minor/     Size    Size   Total           Time GC took
Elapsed   Full GC    Before  After  Available space

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.

  1. PSYoungGen refers to the garbage collector in use for the minor collection. PS stands for Parallel Scavenge.
  2. 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)
  3. 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
jinfo.exe -flag

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
JAVA_OPTS=”-Xms5000m -Xmx13000m


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s