那些年,咱們遇到的OOM

JVM內存模型

先聊聊jvm內存模型,在網上找到一張有直接內存的圖片,方便後面討論java

這張圖真是常看常新,今天咱們從內存溢出的角度從新再審視一遍。 方法區,也稱非堆,hotspot中,1.7叫perm區,1.8叫元空間,所以這個區域溢出,1.7就是OutOfMemoryError: PermGen space,1.8是OutOfMemoryError: Metaspace數組

堆溢出比較常見,OutOfMemoryError: Java heap spacebash

虛擬機棧的溢出是 StackOverflowErrorapp

本地方法棧會出現 java.lang.OutOfMemoryError : unable to create new native Threadjvm

直接內存會拋出 OutOfMemoryError: Direct buffer memoryui

如下將逐個撐爆這些空間spa

先來撐爆虛擬機棧 StackOverflowError

不停地遞歸調用,jvm不得不在虛擬機棧上分配棧幀空間,從而致使sofe,感興趣的還能夠查看一下遞歸的次數,可能經過-Xss進行配置,經過命令jinfo -flag ThreadStackSize [pid]能夠查看棧空間大小的配置線程

public class StackOverflowErrorDemo {
    private static void test() {
        test();
    }

    public static void main(String[] args) {
        test();
    }
}
複製代碼

再來撐爆堆吧 OutOfMemoryError: Java heap space

先指定-Xmx8m -Xms8m,而後直接在堆中生成一個8m字節的數組,能夠直接看到效果3d

public class JavaHeapSpaceDemo {
    public static void main(String[] args) {
        byte[] bytes = new byte[8 * 1014 * 1024];
    }
}
複製代碼

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at com.meituan.waimai.jvm.JavaHeapSpaceDemo.main(JavaHeapSpaceDemo.java:11)code

方法區有分說

若是是1.7,會出現PemGen space, 這要求咱們不斷往生成類的信息。因爲1.7,字符串常量池已經挪到堆中了,因此使用String.intern()並不會致使perm區溢出

public class OOMDemo {
    public static void main(String[] args) {
        String str = "hello";
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            str += (i + "").intern();
            list.add(str);
        }
    }
}
複製代碼

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3332) at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448) at java.lang.StringBuilder.append(StringBuilder.java:136) at com.meituan.waimai.jvm.JavaHeapSpaceDemo.main(JavaHeapSpaceDemo.java:18)

那要怎麼作呢,這裏咱們須要動態地生成一些類,直到把perm區撐爆,jvm參數配置:-XX:MaxPermSize=8m

static class OOMTest{}

    public static void main(final String[] args) {
        int i = 0;

        try {
            for (; ; ) {
                i++;
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(OOMTest.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return methodProxy.invokeSuper(o, args);
                    }
                });
                enhancer.create();
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}
複製代碼

Caused by: java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) ... 8 more

將jdk改成1.8,-XX:MaxMetaspaceSize=20m 從新執行上述程序,結果變成Metaspace溢出

java.lang.OutOfMemoryError: Metaspace at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345) at net.sf.cglib.proxy.Enhancer.generate(Enhancer.java:492) at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:114) at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:480) at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:305) at com.meituan.waimai.jvm.JavaHeapSpaceDemo.main(JavaHeapSpaceDemo.java:36)

該對直接內存動手的

配置jvm參數,-XX:MaxDirectMemorySize=8m,而後分配9m的直接內存:

public class DirectMemoryDemo {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(9 * 1024 * 1024);
    }
}
複製代碼

能夠看到

Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory at java.nio.Bits.reserveMemory(Bits.java:694) at java.nio.DirectByteBuffer.(DirectByteBuffer.java:123) at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311) at com.meituan.waimai.jvm.DirectMemoryDemo.main(DirectMemoryDemo.java:13)

本地方法棧 unable to create new native Thread

不停地建立java線程,就能夠把本地方法棧撐爆

public class NativeThreadDemo {
    public static void main(String[] args) {

        for (; ; ) {
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
複製代碼

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:717) at com.meituan.waimai.jvm.NativeThreadDemo.main(NativeThreadDemo.java:21)

這個程序跑完忘了關,結果一會mac重啓了,很是可怕。。。。 關於unable to create new native Thread,知乎上有個很是深入地討論 www.zhihu.com/question/64…

相關文章
相關標籤/搜索