Saturday, July 8, 2017

Java GC



Parallel Acavenge collector  = "Througput first" collector


This collector shorten the GC time as much as it can , eg: 10s collect 10MB to  5s collect 6MB, GC more frequently!


-XX:MaxGCPauseMillis   // max GC pause time, >0,= GC speed,but speed faster, GC quantity less。

-XX:GCTimeRatio  //throughput time limit (time of code running / (time of code running + GC time))100> x >0, ratio of GC time in total time = reciprocal of throughput,default : 99,eg : -XX:GCTimeRatio=19 则:if MaxGCPauseMillis =1, 1/(1+19)= 5%,
//max GC time occupying total time :5%,limited!default : 1%。

{
     -Xmn //manual set the new generation size
     -XX: SurviorRatio // manual set the rate between Eden and Survivor
     -XX:PretenureSizeThreshold // manual set the age into tenure
}

-XX:+UseAdaptiveSizePolicy //dynamicly allocate above 3 

-Xmx // max heap size

CMS Collector

Cuncurrent Mark Sweep = shorten the GC time , harder than the above "parallel scavenge"

Disadvantage: compete CPU resource with application and leaving lots of  fragments


-XX:CMSInitiatingOccupancyFraction //GC triggering ratio ,the higher the better, to incrase the GC quantity reduce the frequency the best: 80%。

-XX:+UseCMSCompactAtFullCollection // when CMS can't stand approaching Full GC, auto-fragment combining,default: on。

-XX:+PrintGCDetails // print GC log

G1 collector

GC doesn't follow generation policy , it divides JVM into regions with same size , then recycle from each region.


-XX:+UseG1GC // turn on G1 collector

-XX:G1NewSizePercent // Young Generation ratio on whole heap, default : 5%

-XX:G1MaxNewSizePercent // Yong Generation max ratio on whole heap, default: 60%

-XX:+G1SummarizeRSetStats // print debug JMV message when G1 on
-XX:+G1TraceConcRefinement //print JVM LOG when G1 on
-XX:+PrintGCDetails // print GC log

Eden Space

JVM has 2 areas : heap and non-heap area

Heap include:  Eden space, Survivor Space(s0,s1), Tenured Gen
Non-Heap include: Code cache,  Perm Gen,  Jvm Stack ,  Local Method Stack.


-XX: NewSize // Yong Generation size,suggest to be 1/3 of whole heap

-XX:MaxNewSize // Yong Generation max size

-XX: SurvivorRatio  // ratio between Eden and Survivor ,default 8:1

-XX:+PrintTenuringDistribution  // print after each Minor GC, the size of objects in Survivor

STW(Stop the world)

In a running java application, all threads stop, one GC is running. 


-Xss  // set each thread's heap usage size eg -Xss128k

-XX:+PrintGCDetails // print GC details

-XX:+PrintGCDateStamps // print time 

-XX:+PrintHeapAtGC // before GC,  show Object status in heap

-Xloggc:./gclogs // print GC log into log file

GC Log Parsing


Tenured: //old generation,   
Perm: //perm gen = in method stack , older than tenured gen, not easy to be recycled.
Minor GC://GC in Young Generation.
Full GC:// GC in old generation,10 times slower than GC in young generation.

//Yong generation , in different collectors, names are different:

DefNew : //Default new generation , in  Serial collectors name.
ParNew: //Parallel New Generation , in ParNew collectors name.
PSYoungGen:// in  Parallel Scavenge collactors name.

// combined statrgy:

-Xmn20M // Java Heap size is 20M
-Xmx10M // 10M to Yong Generation 
-Xmn10M // 10M to Old Generation
-XX:SurvivorRatio=8 // set Eden and Survivor ratio 8:1
-verbose:gc  // print JVM GC details,put on the 1st place in all parameters。

// 3 ways into tenured gen:

-XX:MaxTenuringThreshold=15 // in survivor space, stay till 15 years old then into Old Generation
-big object directly into Old Generation,eg: //long String or  byte array

-multiple objects reach 1/2 of survivor space ,directly into Old Generation, though have not reach 15 years old

Check GC in JAVA


byte[] array = new byte[300*1024*1024];

for(MemoryPoolMXBean memoryPoolMXBean: ManagementFactory.getMemoryPoolMXBeans()){
    System.out.println(memoryPoolMXBean.getName());
    System.out.println(memoryPoolMXBean.getUsage().getUsed());
}

// get all JVM parameters
java -XX:+PrintVMOptions -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal  -version

No comments:

Post a Comment

Add Loading Spinner for web request.

when web page is busily loading. normally we need to add a spinner for the user to kill their waiting impatience. Here, 2 steps we need to d...