List<OOMObject> list = new ArrayList<OOMObject>();
while(true){
list.add(new OOMObject());
}
}
}
public class HeapOOM {static class OOMObject{}/*** @param args*/public static void main(String[] args) {List<OOMObject> list = new ArrayList<OOMObject>();while(true){list.add(new OOMObject());}}} 加上JVM參數(shù)-verbose:gc -Xms10M -Xmx10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError,就能很快報(bào)出OOM:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
public class MethodAreaOOM {static class OOMOjbect{}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubwhile(true){Enhancer eh = new Enhancer();eh.setSuperclass(OOMOjbect.class);eh.setUseCache(false);eh.setCallback(new MethodInterceptor(){@Overridepublic Object intercept(Object arg0, Method arg1,Object[] arg2, MethodProxy arg3) throws Throwable {// TODO Auto-generated method stubreturn arg3.invokeSuper(arg0, arg2);}});eh.create();}}} 加上永久帶的JVM參數(shù):-XX:PermSize=10M -XX:MaxPermSize=10M,運(yùn)行后會(huì)報(bào)如下異常:
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
靜態(tài)變量或常量也會(huì)有可能撐爆方法區(qū):
[java] view plain copy print ?
public class ConstantOOM {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
int i=0;
while(true){
list.add(String.valueOf(i++).intern());
}
}
}
public class ConstantOOM {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubList<String> list = new ArrayList<String>();int i=0;while(true){list.add(String.valueOf(i++).intern());}}} 同樣加上JVM參數(shù):-XX:PermSize=10M -XX:MaxPermSize=10M,運(yùn)行后報(bào)如下異常:
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space