(转)JVM各种内存溢出是否产生dump
Posted hadley
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)JVM各种内存溢出是否产生dump相关的知识,希望对你有一定的参考价值。
对于java的内存溢出,如果配置-XX:+HeapDumpOnOutOfMemoryError,很明确的知道堆内存溢出时会生成dump文件。但永久代内存溢出不明确是否会生成,今天来做一个实验:
永久代内存溢出,有dump文件。JVM的参数是-XX:PermSize=10m -XX:MaxPermSize=10m -XX:+HeapDumpOnOutOfMemoryError
-
import java.util.ArrayList;
-
import java.util.List;
-
public class PermPoolOOM{
-
public static void main(String[] args){
-
List list = new ArrayList();
-
int i = 0;
-
while(true){
-
list.add(String.valueOf(i++).intern());
-
}
-
}
-
}
Dumping heap to java_pid8900.hprof ...
Heap dump file created [17009938 bytes in 0.336 secs]
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
at java.lang.String.intern(Native Method)
at PermPoolOOM.main(PermPoolOOM.java:8)
方法区溢出,有dump文件。需要下载cglib-full-2.0.1.jar,JVM参数设置:-XX:PermSize=10m -XX:MaxPermSize=10m -XX:+HeapDumpOnOutOfMemoryError
-
import java.lang.reflect.Method;
-
import net.sf.cglib.proxy.Enhancer;
-
import net.sf.cglib.proxy.MethodInterceptor;
-
import net.sf.cglib.proxy.MethodProxy;
-
-
public class JavaMethodAreaOOM {
-
-
public static void main(String[] args) {
-
while (true) {
-
Enhancer enhancer = new Enhancer();
-
enhancer.setSuperclass(OOMObject.class);
-
enhancer.setUseCache(false);
-
enhancer.setCallback(new MethodInterceptor() {
-
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
-
return proxy.invokeSuper(obj, args);
-
}
-
});
-
enhancer.create();
-
}
-
}
-
static class OOMObject {
-
-
}
-
}
Dumping heap to java_pid11000.hprof ...
Heap dump file created [6752989 bytes in 0.075 secs]
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:208)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:406)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:318)
at JavaMethodAreaOOM.main(JavaMethodAreaOOM.java:21)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:411)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:195)
... 3 more
Caused by: java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
... 8 more
直接内存溢出,没有dump文件。JVM参数配置:-XX:PermSize=10m -XX:MaxPermSize=10m -XX:+HeapDumpOnOutOfMemoryError -XX:MaxDirectMemorySize=10M
-
import java.lang.reflect.Field;
-
import sun.misc.Unsafe;
-
-
public class DirectMemoryOOM{
-
private static final int _1MB = 1024* 1024 * 1024;
-
public static void main(String[] args) throws Exception{
-
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
-
unsafeField.setAccessible(true);
-
Unsafe unsafe = (Unsafe) unsafeField.get(null);
-
while(true){
-
//unsafe直接想操作系统申请内存
-
unsafe.allocateMemory(_1MB);
-
}
-
}
-
}
at sun.misc.Unsafe.allocateMemory(Native Method)
at com.comtop.DirectMemoryOOM.main(DirectMemoryOOM.java:15)
栈内存溢出,没有dump文件。JVM参数设置:-Xss128k
-
public class JavaVMStackOF{
-
int stackLength = 1;
-
public void stackLeak(){
-
stackLength++;
-
stackLeak();
-
}
-
public static void main(String[] args){
-
JavaVMStackOF oom = new JavaVMStackOF();
-
oom.stackLeak();
-
}
-
}
Exception in thread "main" java.lang.StackOverflowError
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:6)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
at JavaVMStackOF.stackLeak(JavaVMStackOF.java:7)
以上是关于(转)JVM各种内存溢出是否产生dump的主要内容,如果未能解决你的问题,请参考以下文章