publicstaticvoidmain(String[] args) throws IOException { // list --> WeakReference --> byte[] List<WeakReference<byte[]>> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { WeakReference<byte[]> reference = new WeakReference<>(newbyte[_4MB]); list.add(reference); for (WeakReference<byte[]> w : list){ System.out.println(w.get()+" "); } System.out.println(); } System.out.println("循环结束:"+list.size()); } }
循环结束:5 Heap PSYoungGen total 6144K, used 4265K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000) eden space 5632K, 75% used [0x00000000ff980000,0x00000000ffdaa5b8,0x00000000fff00000) from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) ParOldGen total 11776K, used 5076K [0x00000000fec00000, 0x00000000ff780000, 0x00000000ff980000) object space 11776K, 43% used [0x00000000fec00000,0x00000000ff0f53b0,0x00000000ff780000) Metaspace used 3281K, capacity 4500K, committed 4864K, reserved 1056768K class space used 355K, capacity 388K, committed 512K, reserved 1048576K
Heap def new generation total 9216K, used 2982K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) eden space 8192K, 36% used [0x00000000fec00000, 0x00000000feee9818, 0x00000000ff400000) from space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000) to space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000) tenured generation total 10240K, used 0K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) the space 10240K, 0% used [0x00000000ff600000, 0x00000000ff600000, 0x00000000ff600200, 0x0000000100000000) Metaspace used 3273K, capacity 4496K, committed 4864K, reserved 1056768K classspaceused354K, capacity388K, committed512K, reserved1048576K
public static void main(String[] args) { ArrayList<byte[]> list = new ArrayList<>(); list.add(new byte[_7MB]); } }
打印结果如下:
1 2 3 4 5 6 7 8 9 10
[GC (Allocation Failure) [DefNew: 2981K->980K(9216K), 0.0017746 secs]2981K->980K(19456K), 0.0018187 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] Heap def new generation total 9216K, used 8394K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) eden space 8192K, 90% used [0x00000000fec00000, 0x00000000ff33d8c0, 0x00000000ff400000) from space 1024K, 95% used [0x00000000ff500000, 0x00000000ff5f5228, 0x00000000ff600000) to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000) tenured generation total 10240K, used 0K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) the space 10240K, 0% used [0x00000000ff600000, 0x00000000ff600000, 0x00000000ff600200, 0x0000000100000000) Metaspace used 3274K, capacity 4496K, committed 4864K, reserved 1056768K classspaceused354K, capacity388K, committed512K, reserved1048576K
public static void main(String[] args) { ArrayList<byte[]> list = new ArrayList<>(); list.add(new byte[_8MB]); } }
打印结果:
1 2 3 4 5 6 7 8 9
Heap def new generation total 9216K, used 3145K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) eden space 8192K, 38% used [0x00000000fec00000, 0x00000000fef127b8, 0x00000000ff400000) from space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000) to space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000) tenured generation total 10240K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) the space 10240K, 80% used [0x00000000ff600000, 0x00000000ffe00010, 0x00000000ffe00200, 0x0000000100000000) Metaspace used 3274K, capacity 4496K, committed 4864K, reserved 1056768K classspaceused354K, capacity388K, committed512K, reserved1048576K
Sets the initial and maximum size (in bytes) of the heap for the young generation (nursery). Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, or g or G to indicate gigabytes. The young generation region of the heap is used for new objects. GC is performed in this region more often than in other regions. If the size for the young generation is too small, then a lot of minor garbage collections are performed. If the size is too large, then only full garbage collections are performed, which can take a long time to complete. Oracle recommends that you keep the size for the young generation greater than 25% and less than 50% of the overall heap size.
评论加载中