常见的OOM

Scroll Down

java.lang.StackOverflowError

深度的加载可能会导致栈内存溢出,可能会导致StackOverflowError

public static void main(String args[])  {
    stackOverFlow();
}

public static void stackOverFlow(){
    stackOverFlow();
}

运行结果

Exception in thread "main" java.lang.StackOverflowError
	at com.wangshouyu.gc.HelloGc.stackOverFlow(HelloGc.java:24)

java.lang.OutOfMemoryError:Java heap space

String str = "abc";

while (true){
    str += str + new Random().nextInt(100000);
    str.intern();
}

执行结果

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:674)
	at java.lang.StringBuilder.append(StringBuilder.java:208)
	at com.wangshouyu.gc.HelloGc.main(HelloGc.java:25)

或者配置虚拟机-Xmx5m -Xms5m创建大对象

byte[] b = new byte[10 * 1024 * 1024];

执行结果

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.wangshouyu.gc.HelloGc.main(HelloGc.java:22)

java.lang.OutOfMemoryError:GC overhead limit exeeded

jvm配置-Xmx10m -Xms10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m

List<String> list = new ArrayList<>();
for(int i=0;;i++){
    list.add(String.valueOf(i).intern());
}

执行结果

[Full GC (Ergonomics) [PSYoungGen: 1024K->1024K(2048K)] [ParOldGen: 7047K->7047K(7168K)] 8071K->8071K(9216K), [Metaspace: 3015K->3015K(1056768K)], 0.0378416 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.lang.Integer.toString(Integer.java:403)
	at java.lang.String.valueOf(String.java:3099)
	at com.wangshouyu.gc.HelloGc.main(HelloGc.java:26)

java.lang.OutOfMemoryError:Direct buffer memory

主要是因为nio引起,设置分配JVM参数-Xmx10m -Xms10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m

long maxDirectMemory = VM.maxDirectMemory() / 1024 / 1024;
System.out.println("配置的maxDirectMemory:"+ maxDirectMemory + "MB");
// 配置为5M,实际使用10M
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10 * 1024 * 1024);

执行结果

配置的maxDirectMemory:5MB
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.wangshouyu.gc.HelloGc.main(HelloGc.java:27)

java.lang.OutOfMemoryError:unable to create new native thread

int i = 0;
while (true){
    i++;
    new Thread(()->{
        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
    System.out.println(i);
}

执行结果

2027
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.wangshouyu.gc.HelloGc.main(HelloGc.java:39)

java.lang.OutOfMemoryError:Metaspace

设置元空间大小-XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m

static class OOMTest{

}

while (true){
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(OOMTest.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();
}

执行结果

Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:467)
	at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336)
	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.wangshouyu.gc.HelloGc.main(HelloGc.java:33)