Serial收集器
在运行时通过-Xms20M、-Xmx20M、-Xmn10M这三个参数限制了Java堆大小为20MB,不可扩展,其中10MB分配给新生代,10MB分配给老年代。
/**
* VM参数: -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SruvivorRation=8 -XX:+UseSerialGC
* -XX:+UseSerialGC 指定新生代使用Serial收集器
*/
public class AllocationTest {
private static final int _1MB =1024 * 1024;
public static void main(String[] args){
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte[2 * _1MB];
allocation2 = new byte[2 * _1MB];
allocation3 = new byte[2 * _1MB];
allocation4 = new byte[4 * _1MB];
}
}
GC日志
[GC[DefNew: 6981K->477K(9216K), 0.0063639 secs] 6981K->6621K(19456K), 0.0064132 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]
Heap
def new generation total 9216K, used 4983K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 55% used [0x00000000f9a00000, 0x00000000f9e667b0, 0x00000000fa200000)
from space 1024K, 46% used [0x00000000fa300000, 0x00000000fa377768, 0x00000000fa400000)
to space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
tenured generation total 10240K, used 6144K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 60% used [0x00000000fa400000, 0x00000000faa00030, 0x00000000faa00200, 0x00000000fae00000)
compacting perm gen total 21248K, used 2551K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 12% used [0x00000000fae00000, 0x00000000fb07dea8, 0x00000000fb07e000, 0x00000000fc2c0000)
No shared spaces configured.
从GC日志中看出,执行中分配allocation4
对象的语句时会发生一次Minor GC,这次GC的结果是新生代使用的内存容量由GC前的6981K变为GC后的477K,而总内存占用量几乎没有减少(因为allocation1
、allocation2
、allocation3
三个对象都是存活的,虚拟机几乎没有找到可以回收的对象)。发生GC的原因是给allocation4
分配内存时,发现Eden已经被占用了6MB,剩余空间已不足以分配allocation4
所需的4MB内存,因此发生了Minor GC。GC期间虚拟机又发现已有的3个2MB大小的对象全部无法放进Survivor空间(1MB),所以通过分配担保机制提前转移到老年代去。
这次GC结束后,4MB的allocation4
对象顺利分配在Eden中,因此程序执行完的结果是Eden占用4MB(allocation4
),Survivor空闲,老年代被占用6MB(allocation1
、allocation2
、allocation3
)。通过GC日志可以证实。
ParNew收集器
VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseParNewGC
GC日志
[GC[ParNew: 6981K->519K(9216K), 0.0833662 secs] 6981K->6663K(19456K), 0.0834041 secs] [Times: user=0.00 sys=0.00, real=0.08 secs]
Heap
par new generation total 9216K, used 5025K [0x00000000f9a00000, 0x00000000fa400000, 0x00000000fa400000)
eden space 8192K, 55% used [0x00000000f9a00000, 0x00000000f9e667b0, 0x00000000fa200000)
from space 1024K, 50% used [0x00000000fa300000, 0x00000000fa381c70, 0x00000000fa400000)
to space 1024K, 0% used [0x00000000fa200000, 0x00000000fa200000, 0x00000000fa300000)
tenured generation total 10240K, used 6144K [0x00000000fa400000, 0x00000000fae00000, 0x00000000fae00000)
the space 10240K, 60% used [0x00000000fa400000, 0x00000000faa00030, 0x00000000faa00200, 0x00000000fae00000)
compacting perm gen total 21248K, used 2551K [0x00000000fae00000, 0x00000000fc2c0000, 0x0000000100000000)
the space 21248K, 12% used [0x00000000fae00000, 0x00000000fb07dea8, 0x00000000fb07e000, 0x00000000fc2c0000)
No shared spaces configured.
Parallel Scavenge收集器
VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseParallelGC
GC日志
Heap
PSYoungGen total 9216K, used 7145K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 87% used [0x00000000ff600000,0x00000000ffcfa498,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
to space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
ParOldGen total 10240K, used 4096K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 40% used [0x00000000fec00000,0x00000000ff000010,0x00000000ff600000)
PSPermGen total 21504K, used 2548K [0x00000000f9a00000, 0x00000000faf00000, 0x00000000fec00000)
object space 21504K, 11% used [0x00000000f9a00000,0x00000000f9c7d300,0x00000000faf00000)
从GC日志中看出,这次执行到allocation4 = new byte[4 * _1MB];
时没有发生GC,Eden区使用时87%内存,稍大于6MB,也就是说allocation1
、allocation2
、allocation3
对象是存活在Eden区的,而剩下的空间不足以分配给4MB的allocation4
,Survivor区仅有1MB也不足以分配给它,此时Parallel Scavenge收集器直接将allocation4
放进老年代中。从GC日志中的ParOldGen使用了4096K刚好证明了这一点。
G1收集器
VM参数:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseG1GC
GC日志
GC pause (young) (initial-mark), 0.0023456 secs]
[Parallel Time: 1.7 ms, GC Workers: 8]
[GC Worker Start (ms): Min: 254.8, Avg: 254.8, Max: 254.9, Diff: 0.1]
[Ext Root Scanning (ms): Min: 0.7, Avg: 0.8, Max: 1.4, Diff: 0.7, Sum: 6.3]
[Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
[Processed Buffers: Min: 0, Avg: 1.0, Max: 8, Diff: 8, Sum: 8]
[Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
[Object Copy (ms): Min: 0.0, Avg: 0.6, Max: 0.7, Diff: 0.7, Sum: 4.6]
[Termination (ms): Min: 0.0, Avg: 0.1, Max: 0.1, Diff: 0.1, Sum: 0.6]
[GC Worker Other (ms): Min: 0.0, Avg: 0.1, Max: 0.1, Diff: 0.1, Sum: 0.6]
[GC Worker Total (ms): Min: 1.5, Avg: 1.5, Max: 1.6, Diff: 0.1, Sum: 12.2]
[GC Worker End (ms): Min: 256.3, Avg: 256.4, Max: 256.4, Diff: 0.0]
[Code Root Fixup: 0.0 ms]
[Clear CT: 0.4 ms]
[Other: 0.3 ms]
[Choose CSet: 0.0 ms]
[Ref Proc: 0.2 ms]
[Ref Enq: 0.0 ms]
[Free CSet: 0.0 ms]
[Eden: 1024.0K(10.0M)->0.0B(9216.0K) Survivors: 0.0B->1024.0K Heap: 6836.8K(20.0M)->6768.1K(20.0M)]
[Times: user=0.00 sys=0.00, real=0.01 secs]
[GC concurrent-root-region-scan-start]
[GC concurrent-root-region-scan-end, 0.0007035 secs]
[GC concurrent-mark-start]
Heap
garbage-first heap total 20480K, used 10864K [0x00000000f9a00000, 0x00000000fae00000, 0x00000000fae00000)
region size 1024K, 2 young (2048K), 1 survivors (1024K)
compacting perm gen total 20480K, used 2551K [0x00000000fae00000, 0x00000000fc200000, 0x0000000100000000)
the space 20480K, 12% used [0x00000000fae00000, 0x00000000fb07dea8, 0x00000000fb07e000, 0x00000000fc200000)
No shared spaces configured.
网友评论