Find current heap size with jmap
Asked Answered
L

2

12

I would like to find out what is the total heap size that is in use at a certain time by a Java process and I have to use jmap.

The output of jmap -heap <pid> gives me something like this:

Attaching to process ID 2899, please wait...
Debugger attached successfully.             
Server compiler detected.                   
JVM version is 14.2-b01                     

using thread-local object allocation.
Parallel GC with 2 thread(s)         

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 1258291200 (1200.0MB)
   NewSize          = 1048576 (1.0MB)      
   MaxNewSize       = 4294901760 (4095.9375MB)
   OldSize          = 4194304 (4.0MB)         
   NewRatio         = 8                       
   SurvivorRatio    = 8                       
   PermSize         = 16777216 (16.0MB)       
   MaxPermSize      = 67108864 (64.0MB)       

Heap Usage:
PS Young Generation
Eden Space:        
   capacity = 119013376 (113.5MB)
   used     = 117277608 (111.84464263916016MB)
   free     = 1735768 (1.6553573608398438MB)
   98.54153536489882% used
From Space:
   capacity = 131072 (0.125MB)
   used     = 81920 (0.078125MB)
   free     = 49152 (0.046875MB)
   62.5% used
To Space:
   capacity = 131072 (0.125MB)
   used     = 0 (0.0MB)
   free     = 131072 (0.125MB)
   0.0% used
PS Old Generation
   capacity = 954466304 (910.25MB)
   used     = 80791792 (77.04905700683594MB)
   free     = 873674512 (833.2009429931641MB)
   8.46460390077846% used
PS Perm Generation
   capacity = 57671680 (55.0MB)
   used     = 41699008 (39.76727294921875MB)
   free     = 15972672 (15.23272705078125MB)
   72.30413263494319% used

Can I use a formula for these values to find out total memory used?

Other suggestions on how can I find this out on Linux are welcome but jmap is preferred over them.

Thanks

Lardy answered 24/2, 2010 at 8:25 Comment(0)
D
3

if you are trying to compare to something like jconsole or jvisualvm with the main window displaying 'total heap usage' I found out that by adding 'used eden space' and 'used ps old generation space' I got the equivalent of what that graph shows in the aforementioned programs -- that's what I tend to go on now.

Defenestration answered 12/7, 2010 at 15:59 Comment(0)
I
2

The entries under Heap Usage: are listing the various partitioned memory pools in the JVM, along with their max size, used and free space. You could just add up the various used: values, that should give you a sensible value for the total memory usage, although there may be some JVM overhead that's not accounted for in the listed pools.

Indraft answered 24/2, 2010 at 8:35 Comment(4)
Running jmap -heap:format=b <pid> and parsing the heap dump file with Memory Analyzing Tool from Eclipse gives me a total usage of 22Mb. I can't find a way to obtain the same total value...Lardy
@MelmanRo: My guess is that the eclipse tool removed all non-reachable (i.e. to-be garbage collected) objects from the calculation, which is a calculation of what the heap size would be if a full GC sweep was run. I maintain that the current actual operating-system memory usage is the totals of the various pools as I described.Indraft
Can this be the source of such a big difference?Lardy
@MelmanRo: That depends how much churn the application is creating. It's possible, yes.Indraft

© 2022 - 2024 — McMap. All rights reserved.