In those years, we encounter OOM

JVM memory model

First talk jvm memory model, on the Internet to find a direct memory pictures, convenient discussed later

This picture is really always look new, and today we overflow from the perspective of memory re-examine it again. District method , also known as non-heap, hotspot, a 1.7 called perm area, called 1.8 yuan space, so the overflow area, is 1.7 OutOfMemoryError: PermGen space, 1.8 is OutOfMemoryError:? Metaspace

Heap overflow more common, OutOfMemoryError: Java heap space

VM stack overflow is StackOverflowError

Native method stacks will java.lang.OutOfMemoryError: unable to create new native Thread

Direct memory will throw OutOfMemoryError: Direct buffer memory

These spaces will explode one by one

First to explode virtual machine stack StackOverflowError

Kept recursive call, jvm have to allocate space on the stack frame stack virtual machines, resulting in sofe, interested can also look at the number of recursive, may be configured through -Xss, the command jinfo -flag ThreadStackSize [pid] can Check the configuration of stack space

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

    public static void main(String[] args) {
        test();
    }
}
复制代码

Explode again heap OutOfMemoryError: Java heap space

First specify -Xmx8m -Xms8m, and then generates the stack directly in a 8m byte array, the effect can be seen directly

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)

Said method area partakers

If it is 1.7, there will be PemGen space, which requires us to continue to generate information to the class. Since 1.7, the string constant pool has been moved to the heap, so use String.intern () does not lead to perm overflow

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)

Then how to do it, where we need to dynamically generate some kind, until the perm area explode, jvm parameters: -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

The jdk to 1.8, -XX: MaxMetaspaceSize = 20m above program is executed again, the result becomes an overflow 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)

The direct memory hands

Jvm configuration parameters, -XX: MaxDirectMemorySize = 8m, 9m and direct memory allocation:

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

can be seen

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)

Native method stacks unable to create new native Thread

Constantly create java threads, you can explode the native method stacks

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)

The program ran forgot to turn off, the result will be a mac restart, and very scary. . . . About unable to create new native Thread, known to have a very profound discussion on almost www.zhihu.com/question/64...

Guess you like

Origin juejin.im/post/5d50db9ce51d453bc52315f4