一个这样的代码文件
public class Hello{
public static void main(String[] args) {
Object obj = new Object();
System.gc();
System.out.println();
obj = new Object();
obj = new Object();
System.gc();
System.out.println();}
}
在控制台下编译
javac .\Hello.java
执行,带-XX:+PrintGCDetails参数,输出GC信息
java -XX:+PrintGCDetails Hello
结果输出如下:
[GC (System.gc()) [PSYoungGen: 2949K->704K(56832K)] 2949K->712K(186880K), 0.0012004 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 704K->0K(56832K)] [ParOldGen: 8K->621K(130048K)] 712K->621K(186880K), [Metaspace: 2548K->2548K(1056768K)], 0.0061554 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[GC (System.gc()) [PSYoungGen: 983K->64K(56832K)] 1604K->685K(186880K), 0.0005705 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 64K->0K(56832K)] [ParOldGen: 621K->621K(130048K)] 685K->621K(186880K), [Metaspace: 2549K->2549K(1056768K)], 0.0066822 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 56832K, used 491K [0x0000000780f80000, 0x0000000784e80000, 0x00000007c0000000)
eden space 49152K, 1% used [0x0000000780f80000,0x0000000780ffafb8,0x0000000783f80000)
from space 7680K, 0% used [0x0000000784700000,0x0000000784700000,0x0000000784e80000)
to space 7680K, 0% used [0x0000000783f80000,0x0000000783f80000,0x0000000784700000)
ParOldGen total 130048K, used 621K [0x0000000702e00000, 0x000000070ad00000, 0x0000000780f80000)
object space 130048K, 0% used [0x0000000702e00000,0x0000000702e9b600,0x000000070ad00000)
Metaspace used 2556K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 274K, capacity 386K, committed 512K, reserved 1048576K
[GC (System.gc()) [PSYoungGen: 2949K->704K(56832K)] 2949K->712K(186880K), 0.0012004 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
第一行表示第一次执行收集。新生代的占用空间由2949K变为704K,总的堆占用空间由2949K变为712K。新生代的可用总空间为56832K =55.5M,由于新生代由两个Survivor和一个Eden组成,一个Survivor的大小和Eden的比例为1:8,所以新生代可用空间为总的新生代空间的90%,所以总的新生代空间为55.5/0.9约为61.7M。
[Full GC (System.gc()) [PSYoungGen: 704K->0K(56832K)] [ParOldGen: 8K->621K(130048K)] 712K->621K(186880K), [Metaspace: 2548K->2548K(1056768K)], 0.0061554 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
第二行进行的是Full GC,这个时间就比Minor GC时间长多了。
总的堆空间为186880K=182.5M,总的新生代空间为61.7M,所以总的新生代占总的堆空间的1/3。即老生代与新生代的空间之比为2:1.