package com.google.prophet.quickstart.jvm;
import java.io.IOException;
import java.util.ArrayList;
public class JVMTunning {
/**
* JVM参数 -Xms130m -Xmx130m -Xmn20m -XX:PermSize=20m -XX:MaxPermSize=20m
* -XX:+UseSerialGC OC 110M NC 20M ED :S0 : S1=16:2:2
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws Exception {
System.out.println("Test Start-----");
Thread.sleep(10000);
System.out.println("JStat0---Initial--");
ArrayList tempObjs = new ArrayList<GCDataObject>();
// 创建100M的内存占用,NC上不够用,肯定要触发MinorGC,促使其放在OC
for (int i = 0; i < 51200; i++) {
GCDataObject gcDataObject = new GCDataObject(2);
tempObjs.add(gcDataObject);
}
System.out.println("JStat1---Create 100M Objects--");
Thread.sleep(10000);
System.out.println("Full GC will Start-----");
// 促使FullGC使得OC上空间被占用100M,剩余10M
System.gc();
tempObjs.size();
System.out.println(tempObjs.size());
tempObjs = null;
System.out.println("JStat2---FullGC后--");
Thread.sleep(10000);
// 创建大约16M的对象,NC上放不下,只能放入OC,OC再放不下只能FullGC
ArrayList tempObj1s = new ArrayList<GCDataObject>();
for (int i = 0; i < 3200; i++) {
GCDataObject gcDataObject = new GCDataObject(5);
tempObj1s.add(gcDataObject);
}
System.out.println(tempObj1s.size());
// 诱发FullGC
tempObj1s = null;
System.out.println("JStat3---Create 16M Objects--");
Runtime.getRuntime().exec("jstat -gc");
Thread.sleep(10000);
Thread.sleep(1000000000);
}
}
class GCDataObject {
RefDataObject refDataObj = null;
byte[] gcByte = null;
public GCDataObject(int i) {
gcByte = new byte[1024 * i];
refDataObj = new RefDataObject();
}
}
class RefDataObject {
ChildDataObject childDataObject = null;
public RefDataObject() {
childDataObject = new ChildDataObject();
}
}
class ChildDataObject {
Object object = null;
public ChildDataObject() {
object = new Object();
}
}
MinorGC是针对新生代中的EC区域的。
如果EC过大,那么MinorGC频度减少,好处是大部分对象可能就在E0区域销毁了,但是如果新堆大那么OC就有可能过小本来应该可以放在OC上的但是由于OC空间太小,导致FullGC,现在不得不在FullGC进行回收,有可能导致FullGC过多
如果EC过小 MinorGC会频繁进行,但是频率太快就会导致回收不到应该回收的对象,对象被放入OC中,OC不够用,则触发FullGC进行。
原则就是能马上回收的就马上回收尽量避免进入下一代,从而增大FullGC的概率。
如果SurvivorRatio调小,会避免直接进入OC