Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check heap usage of a running JVM from the command line?

Can I check heap usage of a running JVM from the commandline, I mean the actual usage rather than the max amount allocated with Xmx.

I need it to be commandline because I don't have access to a windowing environment, and I want script based on the value , the application is running in Jetty Application server

like image 348
Paul Taylor Avatar asked Jan 22 '13 17:01

Paul Taylor


People also ask

How do I check my current Java heap?

You can verify that the JVM is using the increased Java heap space: Open a terminal window. Review the command output. The argument beginning with "-Xmx" will give you the value of the current Java heap space.

How do I check my Java memory usage?

Using VisualVM (jvisualvm) jvisualvm is a tool to analyse the runtime behavior of your Java application. It allows you to trace a running Java program and see its the memory and CPU consumption. You can also use it to create a memory heap dump to analyze the objects in the heap.

What is JVM heap usage?

The Java™ virtual machine (JVM) heap is an independent memory allocation that can reduce the capacity of the main memory heap. Every integration server creates its own JVM. The integration server uses the JVM to execute the internal administration threads that require Java. This usage is typically minimal.


2 Answers

You can use jstat, like :

 jstat -gc pid 

Full docs here : http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html

like image 167
Mark Avatar answered Sep 23 '22 03:09

Mark


For Java 8 you can use the following command line to get the heap space utilization in kB:

jstat -gc <PID> | tail -n 1 | awk '{split($0,a," "); sum=a[3]+a[4]+a[6]+a[8]; print sum}' 

The command basically sums up:

  • S0U: Survivor space 0 utilization (kB).
  • S1U: Survivor space 1 utilization (kB).
  • EU: Eden space utilization (kB).
  • OU: Old space utilization (kB).

You may also want to include the metaspace and the compressed class space utilization. In this case you have to add a[10] and a[12] to the awk sum.

like image 39
Till Schäfer Avatar answered Sep 25 '22 03:09

Till Schäfer