Sunday, September 20, 2015

How to troubleshoot GC run



Memory setting is very important for a java application.If properly set it will give significant clue when there is a performance issue from jvm side.

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

Saturday, September 5, 2015

What are all different memory areas in JVM

Permanent Generation:
-Contains the application metadata required by the JVM to describe the classes and methods used in the application. Note that Perm Gen is not part of Java Heap memory.
Method Area:
- Part of space in the Perm Gen and used to store class structure (runtime constants and static variables) and code for methods and constructors

Runtime Constant Pool
-per-class runtime representation of constant pool in a class. 
It contains class runtime constants and static methods. 
Runtime constant pool is the part of method area.

Memory Pool
-Created by JVM memory managers to create a pool of immutable objects, if implementation supports it
-Memory Pool can belong to Heap or Perm Gen, depending on the JVM memory manager implementation.
Eg. String pool

Young Generation
- where all the new objects are created. 
 This garbage collection is called Minor GC. Young Generation is divided into three parts – Eden Memory and two Survivor Memory spaces.
Since Young generation keeps short-lived objects, Minor GC is very fast and the application doesn’t get affected by this.

Old Generation:
contains the objects that are long lived and survived after many rounds of Minor GC. 
 Old Generation Garbage Collection is called -Major GC and usually takes longer time.

Stop the World Event
All the Garbage Collections are “Stop the World” events because all application threads are stopped until the operation completes.
Minor GC is very fast and the application doesn’t get affected by this.
Major GC takes longer time because it checks all the live objects
 Perm Gen objects are garbage collected in a full garbage collection.

How to monitor java memory


Java memory management is very important as this will give the health of our application.

Basic options:
Heap: -Xms512m –Xmx512m
Permanent generation:-XX:PermSize=128m -XX:MaxPermSize=256m  
Heap free ratio:-XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70  

Garbage collectiom
Young generation:
NewRatio:8  -XX:newRatio=8 
Survivor ratio:32   -XX:survivorRatio=32

Garbage collector type: 
G1 --> -XX:+UseG1GC  
ConcurrentMarkAndSweep(old) -> -XX:+UseConcMarkSweepGC 
Garbage collector:-XX:+UseG1GC  

Visual tool : jvisualvm
The below command gives the memory allocation
 jstat -gc <process_id> <time_interval>
 jstat -gc 6781 30000

 S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT   
264192.0 278016.0 180332.1  0.0   842240.0 578974.5  292864.0   214754.4  38400.0 36880.1     18    2.663   1      0.423    3.087
264192.0 278016.0 180332.1  0.0   842240.0 813343.0  292864.0   214754.4  38400.0 36884.6     18    2.663   1      0.423    3.087
where columns are
S0C and S1C:  Current size of the Survivor0 and Survivor1 areas in KB.

S0U and S1U: Current usage of the Survivor0 and Survivor1 areas in KB. One of the survivor areas are empty all the time.

EC and EU:  Current size and usage of Eden space in KB. EU size will keep increasing and as soon as it crosses the EC, Minor GC is called and EU size is decreased.

OC and OU: Current size and current usage of Old generation in KB.

PC and PU: Current size and current usage of Perm Gen in KB.

YGC and YGCT: YGC tells the number of GC event occurred in young generation. YGCT gives the accumulated time for GC operations for Young generation. Both YGC and YGCT will increase in the same row where EU value is dropped because of minor GC.

FGC and FGCT: FGC column is the number of Full GC event occurred. FGCT column gives accumulated time for Full GC operations.Full GC time will be too high when compared to young generation GC timings.

GCT: The total accumulated time for GC operations. Notice that it’s sum of YGCT and FGCT column values.


 If lot of Full GC operations, > increase Old generation memory space
java.lang.OutOfMemoryError: PermGen space errors ==> increase the Perm Gen memory space using -XX:PermGen and -XX:MaxPermGen JVM options More:http://jvmmemory.com/