Whenever there is a memory issue occurring in a java application first thing that comes in our mind is whether the memory setting given is sufficient.
We specify memory configuration with -mx argument while running the java process.
-Investigate JVM memory usage in case of out of memory
-Xmx<max_memory_value>
Garbage collection logging flag:
- -XX:+PrintGC or -verbose:gc
- -XX:+PrintGCDetails
- -XX:+PrintGCTimeStamps and -XX:+PrintGCDateStamps
- -Xloggc //-Xloggc:<file>
How to debug GC run:
Log diagnostic information after each garbage collector run
Run application with -verbose:gc
[GC 69068K->56180K(116544K), 0.0054920 secs]
[GC 69068K->56180K(116544K), 0.0054920 secs]
// Young generation usage before 69068K
and after run 56180K
Size of JVM heap 116544K
[Full GC 99119K->50845K(116544K), 0.0137490 secs] //
The above shows full garbage collection. When application is having frequent full GC running means there is a problem.
And even after full GC run memory utilisation is not going down that needs to be addressed on priority.
How to get heapdump?
1. Using jmap
jmap -F -dump:format=b,file=heap.bin <process_id>
2. Getting a heap dump on OutOfMemoryError
java -Xmx64m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.bin
How to read Heap dump?
I. Using jhat
jhat heap.bin
Access to http://localhost:7000/ to explore the heap dump
II. Using visual tool
jvisualvm
upload the heap dump
Manageable Flags:
All the above flags that w start with “PrintGC” belong to the “manageable category.
eg. The verbose:gc option can be enabled at run time using java management API or JVM TI.Jconsole can also be used to enable this option as it is complaint with JMX extension.
More about JVM diagnostic :http://www.oracle.com/technetwork/java/javase/clopts-139448.html
Manageable Flags:
All the above flags that w start with “PrintGC” belong to the “manageable category.
eg. The verbose:gc option can be enabled at run time using java management API or JVM TI.Jconsole can also be used to enable this option as it is complaint with JMX extension.