(转)JVM各种内存溢出是否产生dump

Posted hadley

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)JVM各种内存溢出是否产生dump相关的知识,希望对你有一定的参考价值。

     对于java的内存溢出,如果配置-XX:+HeapDumpOnOutOfMemoryError,很明确的知道堆内存溢出时会生成dump文件。但永久代内存溢出不明确是否会生成,今天来做一个实验:

永久代内存溢出,有dump文件。JVM的参数是-XX:PermSize=10m  -XX:MaxPermSize=10m   -XX:+HeapDumpOnOutOfMemoryError

 

  1.  
    import java.util.ArrayList;
  2.  
    import java.util.List;
  3.  
    public class PermPoolOOM{
  4.  
    public static void main(String[] args){
  5.  
    List list = new ArrayList();
  6.  
    int i = 0;
  7.  
    while(true){
  8.  
    list.add(String.valueOf(i++).intern());
  9.  
    }
  10.  
    }
  11.  
    }
java.lang.OutOfMemoryError: PermGen space
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

 

  1.  
    import java.lang.reflect.Method;
  2.  
    import net.sf.cglib.proxy.Enhancer;
  3.  
    import net.sf.cglib.proxy.MethodInterceptor;
  4.  
    import net.sf.cglib.proxy.MethodProxy;
  5.  
     
  6.  
    public class JavaMethodAreaOOM {
  7.  
     
  8.  
    public static void main(String[] args) {
  9.  
    while (true) {
  10.  
    Enhancer enhancer = new Enhancer();
  11.  
    enhancer.setSuperclass(OOMObject.class);
  12.  
    enhancer.setUseCache(false);
  13.  
    enhancer.setCallback(new MethodInterceptor() {
  14.  
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
  15.  
    return proxy.invokeSuper(obj, args);
  16.  
    }
  17.  
    });
  18.  
    enhancer.create();
  19.  
    }
  20.  
    }
  21.  
    static class OOMObject {
  22.  
     
  23.  
    }
  24.  
    }
java.lang.OutOfMemoryError: PermGen space
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

 

  1.  
    import java.lang.reflect.Field;
  2.  
    import sun.misc.Unsafe;
  3.  
     
  4.  
    public class DirectMemoryOOM{
  5.  
    private static final int _1MB = 1024* 1024 * 1024;
  6.  
    public static void main(String[] args) throws Exception{
  7.  
    Field unsafeField = Unsafe.class.getDeclaredFields()[0];
  8.  
    unsafeField.setAccessible(true);
  9.  
    Unsafe unsafe = (Unsafe) unsafeField.get(null);
  10.  
    while(true){
  11.  
    //unsafe直接想操作系统申请内存
  12.  
    unsafe.allocateMemory(_1MB);
  13.  
    }
  14.  
    }
  15.  
    }
Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at com.comtop.DirectMemoryOOM.main(DirectMemoryOOM.java:15)

 

栈内存溢出,没有dump文件。JVM参数设置:-Xss128k

 

  1.  
    public class JavaVMStackOF{
  2.  
    int stackLength = 1;
  3.  
    public void stackLeak(){
  4.  
    stackLength++;
  5.  
    stackLeak();
  6.  
    }
  7.  
    public static void main(String[] args){
  8.  
    JavaVMStackOF oom = new JavaVMStackOF();
  9.  
    oom.stackLeak();
  10.  
    }
  11.  
    }

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的主要内容,如果未能解决你的问题,请参考以下文章

JVM各种情况内存溢出分析

关于jvm内存溢出的问题

JVM内存溢出环境备份方法

JVM 线上故障排查基本操作--内容问题排查

jvm内存快照dump文件太大,怎么分析

jvm内存快照dump文件太大,怎么分析