Java – Find current heap size with jmap

javajmaplinuxmemory

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

Best Answer

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.