If i want to test the amount of Memory used by my JVM. Which of the following method is more Reliable / Close to the actual Memory Used.
Runtime s_runtime = Runtime.getRuntime ();
Used_memory = s_runtime.totalMemory () - s_runtime.freeMemory ();
Using the TOP command in Unix
I see a difference in the values when i compare them.
Can anyone help me in identifying, what may be the reason.
Thanks in Advance.
If you are using the SUN JDK I would suggest taking a look at the -jvmstat
Rob MisekTangosol, Inc.Coherence
: It just works.
Of course top (or ps) gives different result.
the Runtime memory details are those of the java object heap, not the os level process memory usage.
jvm process includes all natively allocated blocs, jit compiled code (a lot), handles (sockets, files), graphics ressource (heavyweight gui elements like panels, frame, windows, dialogs), etc...
And "total" is only the jvm current heap, not your machine total.
total-free only gives the possible space before next heap expansion.
You would be better using maxMemory for a constant max heap size. And that is still not the os space. your max heap may be too small, in which case you will get standard OutOfMemoryError, but if your max heap is too large and not sustained by the OS, your jvm may die and be defunct, or the os may kill and let defunct random processes.
So really, if your goal is to change code behavior according to memory usage, you better use Runtime.maxMemory() and make sure your -Xmx##m is set to a sane value.
All - Thanks for your information on this. I have the following questions:
The following is the top commands output, i am trying to understand what is shown under SIZE & RES. Is RES is the actual memory used by JVM and Size is the Maximum Heap Size alloted ( ihave alloted 3 GB as max, since the data cached is to the size of 2.5 GB ).
Also i am not sure if Solaris shows extra Memory for the PID other than used by the JVM ( refere PID 29135 )
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
29135 wasadmin 314 40 0 3273M 2897M cpu/0 53.6H 15.58% java
There are so many memory fields accessible other than the default ones.
Forget top. Use "ps -o..." for using specific field from literally near a 100 fields (linux ps at least).
Doc is not very good about those fields. But you may find more sense than me.
At this point, it's a good question, just in the wrong forum...!
Guys - Figured it out by reading & trying out different options in my Heap Size
1. The OS always allocates contiguous memory space to the JVM
2. When there are more objects created & killed very frequently, the JVM stores them in memory spaces wherever they have free space.
3. This results in a situation called Heap Fragmentation ( same like disk fragmentation ) wherein the heap is fragmented.
4. This makes the JVM not to release the contiguous memory space to OS ( remember, memory is allocated /de-allocated in contiguous memory blocks)
How to Avoid Heap Fragmentation
1.Identify and reduce the frequent creation of unnecessary objects.
2.Compact the Heap by using the option -compactGc option of the JVM ( this comes with a performance penalty )
3. Keep the Max-Heap size setting to as close as possible to the maximum memory required by your JVM
Thanks to everyone's help on this
If i want to test the amount of Memory used by my JVM. .
Maybe, the simplest option is to launch the JVM with the parameter '-verbosegc'. You can improve the information of verbosegc in order to show information about the generations (eden, old, etc.) so maybe with this information you can collect all the data you need.
Jose R. Huergahttp://www.terra.es/personal/jrhuerga