JAVA常见内存溢出模拟

独自空忆成欢 提交于 2020-03-12 08:43:09

JAVA常见内存溢出模拟

1、堆溢出

import java.util.ArrayList;
import java.util.List;

/**
 * 堆溢出
 * -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 * java.lang.OutOfMemoryError: Java heap space
 * Created by double on 2019/8/31.
 */
public class HeapOOM {
    public static void main(String[] args) {
        List<Object> list = new ArrayList<Object>();
        while (true) {
            list.add(new Object());
        }

    }
}

2、虚拟机栈溢出

/**
 * 虚拟机栈溢出
 * -Xss128k
 * java.lang.StackOverflowError
 * Created by double on 2019/8/31.
 */
public class JavaVMStackSOF {
    private int stackLength = 1;

    public void stackLeak() {
        stackLength++;
        stackLeak();
    }

    public static void main(String[] args) throws Throwable {
        JavaVMStackSOF javaVMStackSOF = new JavaVMStackSOF();
        try {
            javaVMStackSOF.stackLeak();
        } catch (Throwable e) {
            System.out.println("stack length: " + javaVMStackSOF.stackLength);
            throw e;
        }
    }
}

3、方法区溢出

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * VM Args(jdk1.8): -XX:MetaspaceSize=30M -XX:MaxMetaspaceSize=30M
 * Exception(jdk1.8): java.lang.OutOfMemoryError: Metaspace
 * 方法区溢出(动态Class太多)
 * Created by double on 2019/8/31.
 */
public class JavaMethodAreaOOM {
    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObcect.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();
        }
    }

    static class OOMObcect {
    }
}

4、方法区中运行时常量池溢出

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 方法区中运行时常量池溢出
 * VM Args(jdk1.6): -XX:PermSize=10M -XX:MaxPermSize=10M
 * VM Args(jdk1.7): -Xms30m -Xmx30m -XX:+HeapDumpOnOutOfMemoryError
 * Exception(jdk1.6) : java.lang.OutOfMemoryError: PermGen space
 * Exception(jdk1.7): java.lang.OutOfMemoryError: Java heap space
 * Created by double on 2019/8/31.
 */
public class RuntimeConstantPoolOOM {
    public static void main(String[] args) {
        //使用List保持着常量池引用,避免Full GC回收常量池行为
        List<String> list = new ArrayList<String>();
        while (true) {
            list.add(UUID.randomUUID().toString().intern());
        }
    }
}

5、本机直接内存溢出

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * 本机直接内存溢出
 * VM Args: -Xmx20M -XX:MaxDirectMemorySize=10M
 * java.lang.OutOfMemoryError
 * sun.misc.Unsafe.allocateMemory(Native Method)
 * Created by double on 2019/8/31.
 */
public class DirectMemoryOOM {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!