我的代碼:爲什麼選擇OOM?首先gc爲什麼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
嗯,你正在分配1050萬'Byte'對象,你需要160+ MB ... – xTrollxDudex
@xTrollxDudex'new Byte [2 * _1mb]'只分配一個對象:2097152引用的數組, null'。假設一個引用是4個字節(32位或CompressedOops),這意味着8 MB(+對象頭)。 – Andreas
啊,空引用...我的壞。 – xTrollxDudex