Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find current heap size with jmap

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

like image 394
fmoga Avatar asked Feb 24 '10 08:02

fmoga


People also ask

What is the current Java heap size?

The current capacity of the metaspace is around 13.5 MB (13724 K). From that 13.5 MB, around 12.5 MB (12983 K) is used. Also, we can have up to 1 GB of metaspace (1048576 K). Moreover, 13842 KB guaranteed to be available for use by the Java virtual machine, also known as committed memory.

How do I check my JVM 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.


2 Answers

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.

like image 80
eyberg Avatar answered Sep 22 '22 05:09

eyberg


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.

like image 26
skaffman Avatar answered Sep 20 '22 05:09

skaffman