-3

My code:

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];
  }
}

Result:

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

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
s_peng
  • 1
  • 2
  • 1
    Um well you are allocating 10.5 million `Byte` objects, you need 160+ MB for that... –  Aug 23 '17 at 03:17
  • 2
    @xTrollxDudex `new Byte[2*_1mb]` only allocates one object: The array of 2097152 references, all `null`. Assuming a reference is 4 bytes (32-bit or CompressedOops), that means 8 MB (+ object header). – Andreas Aug 23 '17 at 04:03
  • Ah, null references... My bad. –  Aug 23 '17 at 23:43

1 Answers1

0

A Byte[] is an array of Object References.

An object reference is usually 4 bytes. On a 64-bit machine with more then 32 GB of heap, an object reference is 8 bytes.

So, assuming 4 bytes, 2 * 1024 * 1024 * 4 = 8 MB.

So:

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

With only -Xmx20m you run out of memory.

You probably meant for new Byte[2*_1mb] to allocate 2 MB, so change Byte to byte, so the array is an array of Primitive byte values, not an array of Object References.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • tks,thank you for An object reference is usually 4 bytes. – s_peng Aug 23 '17 at 04:20
  • sorry,i hava a another question. i change Byte to byte. first gc : DefNew: 6636K->781K(9216K) and finally eden space 8192K, 77% used,tenured generation total 10240K, used 4096K. i think byte[4*1Mb] should allocate def new generation and three byte[2*1Mb] after gc should allocate tenured generation. – s_peng Aug 24 '17 at 02:49