?????????棨Direct Memory??
?????????沢?????????????????????????????????Java??????淶?ж???????????????????????з?????????Java???С??????????????????????????С????????????????????????????????OutOfMemoryError?????????JDK1.4??????????NIO????????????????????????????I/O???????????????????з????????棬????????????棬?????????Щ?????????????????????????Java???Native????????????????
????????ó????????????????????????????????OutOfMemoryError??????DirectMemory???μ?????????????????????????Heap Dump????в?????????????????????????????OOM??Dump?????С????????????£???????????淢?????????
??????????
????Java????????
public class HeapOOM {
static class OOMObject {
}
public static void main(String[] args) {
List<OOMObject> list = new ArrayList<OOMObject>();
while (true) {
list.add(new OOMObject());
}
}
}
?????????????????????????????в???-Xms20m -Xmx20m???ò???????Java???С?20M??????????????н?????£?
<span style="color: #ff0000;">Exception in thread "main" java.lang.OutOfMemoryError: Java heap space</span>
at java.util.Arrays.copyOf(Arrays.java:2245)
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at HeapOOM.main(HeapOOM.java:17)
????????????????????????????????java.lang.OutOfMemoryError????????????????Java heap space??
??????????????????????
??????????????????????????????????????????η?????????????????????????????????С????????????齫????????????????????
StackOverflowError??
public class JavaVMStackSOF {
private int stackLength = 1;
public void stackLeak() {
stackLength++;
stackLeak();
}
public static void main(String[] args) throws Throwable {
JavaVMStackSOF oom = new JavaVMStackSOF();
try {
oom.stackLeak();
} catch (Throwable e) {
System.out.println("stack length:" + oom.stackLength);
throw e;
}
}
}
?????????????????????£?
????stack length:10828
????<span style="color: #ff0000;">Exception in thread "main" java.lang.StackOverflowError</span>
????at JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:10)
????at JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:11)
????at JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:11)
??????????????????????????????????????????????10828???????????????????java.lang.StackOverflowError??
?????????????
????????????????Class?????????????????????????η??????????????????????????????????·??????????CGLib??????????????????????????????
public class JavaMethodAreaOOM {
static class OOMObject {
}
public static void main(String[] args) {
while( true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOMObject. class);
enhancer.setUseCache( false);
enhancer.setCallback( new MethodInterceptor() {
@Override
public Object intercept(Object obj?? Method method?? Object[] args??
MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj?? args);
}
});
enhancer.create();
}
}
}
????????????????????????-XX:PermSize=10M -XX:MaxPermSize=10M?????????С?10M???????java.lang.OutOfMemoryError: PermGen space?????????????????????????????
???????
??????????????Java??????????????????????????????????????????????????????Щ??????????????????Ρ??????Щ??????????????????????????????λ????????????????????????????????