2017-08-23 50 views
-3

我的代碼:爲什麼選擇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

+1

嗯,你正在分配1050萬'Byte'對象,你需要160+ MB ... – xTrollxDudex

+2

@xTrollxDudex'new Byte [2 * _1mb]'只分配一個對象:2097152引用的數組, null'。假設一個引用是4個字節(32位或CompressedOops),這意味着8 MB(+對象頭)。 – Andreas

+0

啊,空引用...我的壞。 – xTrollxDudex

回答

0

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]分配2 MB,所以變化Bytebyte,所以數組是原始byte值的數組,而不是對象引用的數組。

+0

tks,謝謝你一個對象引用通常是4個字節。 –

+0

對不起,我還有一個問題。我將字節更改爲字節。第一個gc:DefNew:6636K-> 781K(9216K),最後eden空間8192K,使用率77%,年齡總計10240K,使用4096K。我認爲字節[4 * 1Mb]應該在gc分配終身代後分配def新一代和三字節[2 * 1Mb]。 –