JVM实战:哪些区域会发生内存溢出?

在这里插入图片描述

内存溢出

在这里插入图片描述

元空间

public class MetaspaceOom {
    
    

    // -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m
    public static void main(String[] args) {
    
    

        long counter = 0;

        while (true) {
    
    
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Car.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
    
    
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    
    
                    if (method.getName().equals("run")) {
    
    
                        System.out.println("启动汽车之前先进行安全检查");
                        return methodProxy.invokeSuper(o, objects);
                    } else {
    
    
                        return methodProxy.invokeSuper(o, objects);
                    }
                }
            });

            Car car = (Car) enhancer.create();
            car.run();

            System.out.println("创建了 " + (++counter) + " 个子类");
        }
    }

    static class Car {
    
    
        public void run() {
    
    
            System.out.println("汽车启动,开始行驶");
        }
    }
}
Caused by: java.lang.OutOfMemoryError: Metaspace
	at java.lang.ClassLoader.defineClass1(Native Method)

虚拟机栈

public class StackOom {
    
    

    private static int stackLength = 0;

    // -Xss1M
    public static void main(String[] args) {
    
    
        StackOom stackOom = new StackOom();
        try {
    
    
            stackOom.invoke();
        } catch (Throwable e) {
    
    
            System.out.println("stack length: " + stackLength);
            throw e;
        }
    }

    public void invoke() {
    
    
        stackLength++;
        invoke();
    }
}
Exception in thread "main" java.lang.StackOverflowError
	at com.javashitang.oomKind.StackOom.invoke(StackOom.java:24)

堆内存

public class HeapOom {
    
    

    private static final int _1MB = 1024 * 1024;

    // -Xms20m -Xmx20m
    public static void main(String[] args) {
    
    
        List<byte[]> list = Lists.newArrayList();
        while (true) {
    
    
            list.add(new byte[_1MB]);
        }
    }
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.javashitang.oomKind.HeapOom.main(HeapOom.java:19)

堆外内存

public class DirectMemoryOom {
    
    

    private static final int _1MB = 1024 * 1024;

    // -XX:MaxDirectMemorySize=2m
    public static void main(String[] args) {
    
    
        ByteBuffer.allocateDirect(3 * _1MB);
    }
}
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:694)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
	at com.javashitang.oomKind.DirectMemoryOom.main(DirectMemoryOom.java:15)

参考博客

猜你喜欢

转载自blog.csdn.net/zzti_erlie/article/details/123488261