很多時(shí)候我們在學(xué)習(xí)JVM時(shí)延欠,往往需要查看JVM的回收日志,查看JVM的相關(guān)運(yùn)行參數(shù)沈跨,這時(shí)候我們可以通過手動(dòng)觸發(fā)的形式獲取JVM的運(yùn)行回收情況由捎。
加上參數(shù) -Xms1024m -Xmx1024m -Xmn512m -XX:+PrintGCDetails。
關(guān)鍵的一行在于 System.gc();
上代碼
public class TestGCRoots01 {
private int _10MB = 10 * 1024 * 1024;
private byte[] memory = new byte[8 * _10MB];
public static void main(String[] args) {
method01();
System.out.println("返回main方法");
System.gc();
System.out.println("第二次GC完成");
}
public static void method01() {
TestGCRoots01 t = new TestGCRoots01();
System.gc();
System.out.println("第一次GC完成");
}
}
運(yùn)行結(jié)果
[GC (System.gc()) [PSYoungGen: 97648K->624K(458752K)] 97648K->82552K(983040K), 0.0805172 secs] [Times: user=0.06 sys=0.04, real=0.08 secs]
[Full GC (System.gc()) [PSYoungGen: 624K->0K(458752K)] [ParOldGen: 81928K->82426K(524288K)] 82552K->82426K(983040K), [Metaspace: 3305K->3305K(1056768K)], 0.0252976 secs] [Times: user=0.02 sys=0.00, real=0.03 secs]
第一次GC完成
返回main方法
[GC (System.gc()) [PSYoungGen: 7864K->64K(458752K)] 90291K->82490K(983040K), 0.0017362 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 64K->0K(458752K)] [ParOldGen: 82426K->494K(524288K)] 82490K->494K(983040K), [Metaspace: 3306K->3306K(1056768K)], 0.0115159 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
第二次GC完成
Heap
PSYoungGen total 458752K, used 23593K [0x00000007a0000000, 0x00000007c0000000, 0x00000007c0000000)
eden space 393216K, 6% used [0x00000007a0000000,0x00000007a170a568,0x00000007b8000000)
from space 65536K, 0% used [0x00000007bc000000,0x00000007bc000000,0x00000007c0000000)
to space 65536K, 0% used [0x00000007b8000000,0x00000007b8000000,0x00000007bc000000)
ParOldGen total 524288K, used 494K [0x0000000780000000, 0x00000007a0000000, 0x00000007a0000000)
object space 524288K, 0% used [0x0000000780000000,0x000000078007bb70,0x00000007a0000000)
Metaspace used 3313K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 365K, capacity 388K, committed 512K, reserved 1048576K