Get Thread and Heap dump – Java


Thread and Heap dumps are useful for debugging any Java programme.

To get thread dump:

By using jstack command we can get thread dump of a Java process.


$ jstack PID

where PID is Java process id for which we want to get thread dump.

To get heap dump:

By using jmap command we can get heap dump of a Java process.


$ jmap -heap PID

where PID is Java process id for which we want to get heap dump.

Install oracle java in Ubuntu


To install Oracle Java in Ubuntu operating system run following commands:

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update

After running above commands you can install any of following versions of Oracle Java with apt-get:

  • oracle-java6-installer
  • oracle-java7-installer
  • oracle-java8-installer

If you want to install oracle-java7-installer user following command:

$ sudo apt-get install oracle-java7-installer


Java difference between Xms and Xmx


There are 2 options specify memory/heap size in java:

  1. -Xms: It sets initial (minimum) heap size for JVM. This means that when you start your program the JVM will allocate this amount of memory instantly.
  2. -Xmx: It sets maximum memory size that the heap can reach for the JVM.

Export Heap size in Linux:

$ export JAVA_OPTS="-Xms256m -Xmx512m"

Above command will set minimum heap size as 256MB, and maximum  heap size as 512MB.

Set Heap size for tomcat in Linux:

Add following line in TOMCAT_HOME/bin/ file

JAVA_OPTS="-Xms512m -Xmx2048m"

After adding restart your tamcat. It will will set minimum heap size as 512MB, and maximum  heap size as 2048MB for your tomcat.