Java教程

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);
        }
    }
}

 原文来自:https://www.cnblogs.com/rerise/p/16202418.html

这篇关于JAVA常见内存溢出模拟的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!