JAVA常見內存溢出模擬

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);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章