pacoverflow
pacoverflow

Reputation: 3881

Jstat and Jvisualvm show different numbers for heap usage/allocation

There is a server on which I cannot run any GUI-based profilers such as jvisualvm to monitor the heap of a running Java process. According to this answer, I can use jstat -gc <pid> and look at the OU and OC columns to find out the heap usage and allocation.

But I just compared jvisualvm and jstat on another server, and they do not show the same numbers for the heap usage and allocation.

Jvisualvm shows that a Java process has about 1GB allocated for the heap and about 500MB of it is used:

enter image description here

But for that same process, jstat shows only about 700MB under the OC column and about 226MB under the OU column:

# jstat -gc 39621
S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT
16384.0 17920.0 3984.0  0.0   322560.0 10320.4   699392.0   226295.2  83968.0 47921.1  11966   66.103  12      3.083   69.186

Why are jvisualvm and jstat showing different numbers for the heap usage and allocation?

Upvotes: 1

Views: 2153

Answers (3)

taitelman
taitelman

Reputation: 700

easy unix command to see current retained heap size via jstat: (it matches jconsole numbers)

jstat -gc <pid> 3000 | awk '{split($0,a," "); print a[3]+a[4]+a[6]+a[8]}'

Upvotes: 0

Kevin Swann
Kevin Swann

Reputation: 1038

For Java 8, the jstat utility has been updated and for

jstat –gc <PID>

has these fields in the output:

S0C     S1C      S0U   S1U     EC       EU        OC         OU        MC      MU      CCSC   CCSU    YGC     YGCT    FGC     FGCT     GCT   

39936.0 29184.0  0.0   29035.4 273920.0 170661.5  699392.0   39235.9   98480.0 97022.7 8624.0 8163.3     63    0.925  38      2.363    3.289

Heap Usage

To get the current heap usage, approximately the value in jVisualJM, the following values need to be added:

S0U: Survivor space 0 utilization (kB)

S1U: Survivor space 1 utilization (kB)

EU: Eden space utilization (kB)

OU: Old space utilization (kB)

Looking at Java 8 Virtual Machine Garbage Collection Tuning Guide, it seems that the heap is made up of the 'Young' generation (Eden & Survivor 0 & 1 spaces) and the 'Old' generation (Old space).

Metaspace Usage

To get the current metaspace usage, approximately the value in jVisualJM, the following size is needed:

MU: Metaspace utilization (kB)

Notes

For whatever reason, I have never been able to get exactly the same values between jstat and jVisualJM but close enough to allow both tools be be used with confidence.

JVM Memory Pools

For more details on the JVM memory pools, see this StackOverflow question & answer which covers Java 7 & 8.

There is also some info in the Java 8 Using JConsole guide but surprisingly, this still mentions the Java 7 Permanent Generation space rather than the Java 8 Metaspace.

Upvotes: 3

pacoverflow
pacoverflow

Reputation: 3881

It turns out that adding up the EU and OU columns of jstat is approximately equal to the heap usage shown in jvisualvm. The EU+OU is always about 12 MB less than what's shown in jvisualvm for the process that I am monitoring. (In the example I provided in my question, jvisualvm is showing 506 MB of heap usage and jstat shows EU+OU = 240 MB. So I must have run jstat right after the heap usage dropped back down to 250 MB.)

Adding up the EC and OC columns of jstat is approximately equal to the heap allocation shown in jvisualvm. The EC+OC is about 45-60 MB less than what's shown in jvisualvm for the process that I am monitoring.

Upvotes: 0

Related Questions