为什么会OOM?首先 gc 为什么终身:8192K->8961K(10240K)?

Why OOM? And first gc why Tenured: 8192K->8961K(10240K)?

我的代码:

public class TestJVmRiZHI{   
/**
 jdk 1.8
 -XX:+UseSerialGC
 -verbose:gc
 -Xms20M
 -Xmx20m
 -Xmn10M
 -XX:+PrintGCDetails
 -XX:SurvivorRatio=8
 * @param args
 */     

  private  static final int _1mb = 1024 * 1024;
  public static void main(String[] args) {
    Byte[] allocation1 = new Byte[2*_1mb];
    Byte[] allocation2 = new Byte[2*_1mb];
    Byte[] allocation3 = new Byte[2*_1mb];
    Byte[] allocation4 = new Byte[4*_1mb];
  }
}

结果:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at controller.TestJVmRiZHI.main(TestJVmRiZHI.java:24)

[GC (Allocation Failure) [DefNew: 2540K->770K(9216K), 0.0034872 secs]

[Tenured: 8192K->8961K(10240K), 0.0071963 secs] 10732K->8961K(19456K),

[Metaspace: 3385K->3385K(1056768K)], 0.0107478 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]

[Full GC (Allocation Failure) [Tenured: 8961K->8943K(10240K), 0.0073261 secs] 8961K->8943K(19456K), [Metaspace: 3385K->3385K(1056768K)], 0.0073536 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]

Heap def new generation total 9216K, used 410K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)

eden space 8192K, 5% used [0x00000000fec00000, 0x00000000fec66800, 0x00000000ff400000)

from space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000)

to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000)

tenured generation total 10240K, used 8943K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)

the space 10240K, 87% used [0x00000000ff600000, 0x00000000ffebbd38, 0x00000000ffebbe00, 0x0000000100000000)

Metaspace used 3429K, capacity 4494K, committed 4864K, reserved 1056768K

class space used 382K, capacity 386K, committed 512K, reserved 1048576

一个 Byte[] 是一个对象引用数组。

一个对象引用通常是4个字节。在堆内存超过 32 GB 的 64 位机器上,一个对象引用是 8 个字节。

所以,假设 4 个字节,2 * 1024 * 1024 * 4 = 8 MB.

所以:

allocation1:  8 MB
allocation2:  8 MB
allocation3:  8 MB
allocation4: 16 MB
             =====
      total: 40 MB

只有 -Xmx20m 你 运行 内存不足。

你可能是想让new Byte[2*_1mb]分配2MB,所以Byte改成byte,所以数组是原始 byte 值,而不是对象引用数组。