jvm System.gc ()


jvm System.gc ()

 

Application: Call System.gc () to notify the virtual machine for garbage collection, trigger full gc, and recycle the new generation, old generation, and meta space

 

**********************************

When using the Parallel old garbage collector, the young generation gc will be triggered by default before the full gc

Available parameters -XX: -ScavengeBeforeFullGC prohibit young generation gc

 

public class Test {

    public static void main(String[] args){
        System.gc();
    }
}

 

********************

Console output

 

Virtual machine parameters: -XX: + UseParallelGC -Xlog: gc *

[0.030s][info][gc,heap,coops] Heap address: 0x0000000081c00000, size: 2020 MB, Compressed Oops mode: 32-bit
[0.032s][info][gc           ] Using Parallel

****************
新生代gc

[0.141s][info][gc,start     ] GC(0) Pause Young (System.gc())
[0.142s][info][gc,heap      ] GC(0) PSYoungGen: 3328K(38400K)->984K(38400K) Eden: 3328K(33280K)->0K(33280K) From: 0K(5120K)->984K(5120K)
[0.142s][info][gc,heap      ] GC(0) ParOldGen: 0K(87552K)->8K(87552K)
[0.142s][info][gc,metaspace ] GC(0) Metaspace: 622K(4864K)->622K(4864K) NonClass: 564K(4352K)->564K(4352K) Class: 58K(512K)->58K(512K)
[0.142s][info][gc           ] GC(0) Pause Young (System.gc()) 3M->0M(123M) 1.521ms
[0.142s][info][gc,cpu       ] GC(0) User=0.00s Sys=0.00s Real=0.00s

****************
full gc:新生代、老年代、元空间进行垃圾回收

[0.142s][info][gc,start     ] GC(1) Pause Full (System.gc())
[0.142s][info][gc,phases,start] GC(1) Marking Phase
[0.144s][info][gc,phases      ] GC(1) Marking Phase 1.561ms
[0.144s][info][gc,phases,start] GC(1) Summary Phase
[0.144s][info][gc,phases      ] GC(1) Summary Phase 0.010ms
[0.144s][info][gc,phases,start] GC(1) Adjust Roots
[0.145s][info][gc,phases      ] GC(1) Adjust Roots 0.735ms
[0.145s][info][gc,phases,start] GC(1) Compaction Phase
[0.147s][info][gc,phases      ] GC(1) Compaction Phase 2.579ms
[0.147s][info][gc,phases,start] GC(1) Post Compact
[0.147s][info][gc,phases      ] GC(1) Post Compact 0.034ms
[0.147s][info][gc,heap        ] GC(1) PSYoungGen: 984K(38400K)->0K(38400K) Eden: 0K(33280K)->0K(33280K) From: 984K(5120K)->0K(5120K)
[0.147s][info][gc,heap        ] GC(1) ParOldGen: 8K(87552K)->925K(87552K)
[0.147s][info][gc,metaspace   ] GC(1) Metaspace: 622K(4864K)->622K(4864K) NonClass: 564K(4352K)->564K(4352K) Class: 58K(512K)->58K(512K)
[0.147s][info][gc             ] GC(1) Pause Full (System.gc()) 0M->0M(123M) 5.075ms
[0.147s][info][gc,cpu         ] GC(1) User=0.00s Sys=0.00s Real=0.01s
[0.148s][info][gc,heap,exit   ] Heap
[0.148s][info][gc,heap,exit   ]  PSYoungGen      total 38400K, used 1664K [0x00000000d5f00000, 0x00000000d8980000, 0x0000000100000000)
[0.148s][info][gc,heap,exit   ]   eden space 33280K, 5% used [0x00000000d5f00000,0x00000000d60a01e8,0x00000000d7f80000)
[0.148s][info][gc,heap,exit   ]   from space 5120K, 0% used [0x00000000d7f80000,0x00000000d7f80000,0x00000000d8480000)
[0.148s][info][gc,heap,exit   ]   to   space 5120K, 0% used [0x00000000d8480000,0x00000000d8480000,0x00000000d8980000)
[0.148s][info][gc,heap,exit   ]  ParOldGen       total 87552K, used 925K [0x0000000081c00000, 0x0000000087180000, 0x00000000d5f00000)
[0.148s][info][gc,heap,exit   ]   object space 87552K, 1% used [0x0000000081c00000,0x0000000081ce7648,0x0000000087180000)
[0.148s][info][gc,heap,exit   ]  Metaspace       used 625K, capacity 4531K, committed 4864K, reserved 1056768K
[0.148s][info][gc,heap,exit   ]   class space    used 58K, capacity 402K, committed 512K, reserved 1048576K

 

Virtual machine parameters: -XX: + UseParallelGC -XX: + ScavengeBeforeFullGC -Xlog: gc *

[0.024s][info][gc,heap,coops] Heap address: 0x0000000081c00000, size: 2020 MB, Compressed Oops mode: 32-bit
[0.025s][info][gc           ] Using Parallel
[0.129s][info][gc,start     ] GC(0) Pause Full (System.gc())
[0.129s][info][gc,phases,start] GC(0) Marking Phase
[0.131s][info][gc,phases      ] GC(0) Marking Phase 1.772ms
[0.131s][info][gc,phases,start] GC(0) Summary Phase
[0.131s][info][gc,phases      ] GC(0) Summary Phase 0.010ms
[0.131s][info][gc,phases,start] GC(0) Adjust Roots
[0.131s][info][gc,phases      ] GC(0) Adjust Roots 0.537ms
[0.131s][info][gc,phases,start] GC(0) Compaction Phase
[0.135s][info][gc,phases      ] GC(0) Compaction Phase 3.561ms
[0.135s][info][gc,phases,start] GC(0) Post Compact
[0.135s][info][gc,phases      ] GC(0) Post Compact 0.053ms
[0.135s][info][gc,heap        ] GC(0) PSYoungGen: 3328K(38400K)->0K(38400K) Eden: 3328K(33280K)->0K(33280K) From: 0K(5120K)->0K(5120K)
[0.135s][info][gc,heap        ] GC(0) ParOldGen: 0K(87552K)->924K(87552K)
[0.135s][info][gc,metaspace   ] GC(0) Metaspace: 614K(4864K)->614K(4864K) NonClass: 556K(4352K)->556K(4352K) Class: 58K(512K)->58K(512K)
[0.135s][info][gc             ] GC(0) Pause Full (System.gc()) 3M->0M(123M) 6.102ms
[0.135s][info][gc,cpu         ] GC(0) User=0.00s Sys=0.00s Real=0.01s
[0.136s][info][gc,heap,exit   ] Heap
[0.136s][info][gc,heap,exit   ]  PSYoungGen      total 38400K, used 1664K [0x00000000d5f00000, 0x00000000d8980000, 0x0000000100000000)
[0.136s][info][gc,heap,exit   ]   eden space 33280K, 5% used [0x00000000d5f00000,0x00000000d60a01f0,0x00000000d7f80000)
[0.136s][info][gc,heap,exit   ]   from space 5120K, 0% used [0x00000000d8480000,0x00000000d8480000,0x00000000d8980000)
[0.136s][info][gc,heap,exit   ]   to   space 5120K, 0% used [0x00000000d7f80000,0x00000000d7f80000,0x00000000d8480000)
[0.136s][info][gc,heap,exit   ]  ParOldGen       total 87552K, used 924K [0x0000000081c00000, 0x0000000087180000, 0x00000000d5f00000)
[0.136s][info][gc,heap,exit   ]   object space 87552K, 1% used [0x0000000081c00000,0x0000000081ce7170,0x0000000087180000)
[0.136s][info][gc,heap,exit   ]  Metaspace       used 624K, capacity 4531K, committed 4864K, reserved 1056768K
[0.136s][info][gc,heap,exit   ]   class space    used 58K, capacity 402K, committed 512K, reserved 1048576K

Note: After using -XX: -ScavengeBeforeFullGC, the young generation of gc is not performed before the full gc

 

 

**********************************

System.gc () uses parallel collection by default, and concurrent collection is not used. Even when using the G1 garbage collector, parallel collection is used.

 

public class Test {

    public static void main(String[] args){
        System.gc();
    }
}

 

********************

Console output

 

Virtual machine parameters: -Xlog: gc *

[0.024s][info][gc,heap] Heap region size: 1M
[0.024s][info][gc,heap,coops] Heap address: 0x0000000081c00000, size: 2020 MB, Compressed Oops mode: 32-bit
[0.027s][info][gc           ] Using G1
[0.057s][info][gc           ] Periodic GC disabled
[0.143s][info][gc,task      ] GC(0) Using 3 workers of 8 for full compaction
[0.143s][info][gc,start     ] GC(0) Pause Full (System.gc())
[0.143s][info][gc,phases,start] GC(0) Phase 1: Mark live objects
[0.144s][info][gc,phases      ] GC(0) Phase 1: Mark live objects 1.117ms
[0.144s][info][gc,phases,start] GC(0) Phase 2: Prepare for compaction
[0.145s][info][gc,phases      ] GC(0) Phase 2: Prepare for compaction 0.449ms
[0.145s][info][gc,phases,start] GC(0) Phase 3: Adjust pointers
[0.145s][info][gc,phases      ] GC(0) Phase 3: Adjust pointers 0.563ms
[0.145s][info][gc,phases,start] GC(0) Phase 4: Compact heap
[0.146s][info][gc,phases      ] GC(0) Phase 4: Compact heap 0.759ms
[0.147s][info][gc,heap        ] GC(0) Eden regions: 2->0(2)
[0.147s][info][gc,heap        ] GC(0) Survivor regions: 0->0(0)
[0.147s][info][gc,heap        ] GC(0) Old regions: 0->2
[0.147s][info][gc,heap        ] GC(0) Archive regions: 0->0
[0.147s][info][gc,heap        ] GC(0) Humongous regions: 0->0
[0.147s][info][gc,metaspace   ] GC(0) Metaspace: 622K(4864K)->622K(4864K) NonClass: 564K(4352K)->564K(4352K) Class: 58K(512K)->58K(512K)
[0.147s][info][gc             ] GC(0) Pause Full (System.gc()) 2M->0M(8M) 4.275ms
[0.147s][info][gc,cpu         ] GC(0) User=0.02s Sys=0.00s Real=0.00s
[0.148s][info][gc,heap,exit   ] Heap
[0.148s][info][gc,heap,exit   ]  garbage-first heap   total 8192K, used 925K [0x0000000081c00000, 0x0000000100000000)
[0.148s][info][gc,heap,exit   ]   region size 1024K, 1 young (1024K), 0 survivors (0K)
[0.148s][info][gc,heap,exit   ]  Metaspace       used 625K, capacity 4531K, committed 4864K, reserved 1056768K
[0.148s][info][gc,heap,exit   ]   class space    used 58K, capacity 402K, committed 512K, reserved 1048576K

Note: Garbage collection uses parallel collection

 

Virtual machine parameters: -Xlog: gc * -XX: + ExplicitGCInvokesConcurrent

[0.030s][info][gc,heap] Heap region size: 1M
[0.031s][info][gc,heap,coops] Heap address: 0x0000000081c00000, size: 2020 MB, Compressed Oops mode: 32-bit
[0.035s][info][gc           ] Using G1
[0.065s][info][gc           ] Periodic GC disabled
[0.169s][info][gc,start     ] GC(0) Pause Young (Concurrent Start) (System.gc())
                             //使用并发收集
[0.170s][info][gc,task      ] GC(0) Using 3 workers of 8 for evacuation
[0.172s][info][gc,phases    ] GC(0)   Pre Evacuate Collection Set: 0.1ms
[0.172s][info][gc,phases    ] GC(0)   Merge Heap Roots: 0.1ms
[0.172s][info][gc,phases    ] GC(0)   Evacuate Collection Set: 1.3ms
[0.172s][info][gc,phases    ] GC(0)   Post Evacuate Collection Set: 0.4ms
[0.172s][info][gc,phases    ] GC(0)   Other: 0.4ms
[0.172s][info][gc,heap      ] GC(0) Eden regions: 2->0(13)
[0.172s][info][gc,heap      ] GC(0) Survivor regions: 0->1(3)
[0.172s][info][gc,heap      ] GC(0) Old regions: 0->0
[0.172s][info][gc,heap      ] GC(0) Archive regions: 0->0
[0.172s][info][gc,heap      ] GC(0) Humongous regions: 0->0
[0.172s][info][gc,metaspace ] GC(0) Metaspace: 603K(4864K)->603K(4864K) NonClass: 548K(4352K)->548K(4352K) Class: 55K(512K)->55K(512K)
[0.172s][info][gc           ] GC(0) Pause Young (Concurrent Start) (System.gc()) 2M->0M(128M) 2.465ms
[0.172s][info][gc,cpu       ] GC(0) User=0.00s Sys=0.00s Real=0.00s
[0.172s][info][gc           ] GC(1) Concurrent Cycle
[0.172s][info][gc,marking   ] GC(1) Concurrent Clear Claimed Marks
[0.172s][info][gc,marking   ] GC(1) Concurrent Clear Claimed Marks 0.015ms
[0.172s][info][gc,marking   ] GC(1) Concurrent Scan Root Regions
[0.173s][info][gc,marking   ] GC(1) Concurrent Scan Root Regions 1.103ms
[0.173s][info][gc,marking   ] GC(1) Concurrent Mark (0.173s)
[0.173s][info][gc,marking   ] GC(1) Concurrent Mark From Roots
[0.173s][info][gc,task      ] GC(1) Using 2 workers of 2 for marking
[0.174s][info][gc,marking   ] GC(1) Concurrent Mark From Roots 0.475ms
[0.174s][info][gc,marking   ] GC(1) Concurrent Preclean
[0.174s][info][gc,marking   ] GC(1) Concurrent Preclean 0.102ms
[0.174s][info][gc,marking   ] GC(1) Concurrent Mark (0.173s, 0.174s) 0.618ms
[0.174s][info][gc,start     ] GC(1) Pause Remark
[0.175s][info][gc           ] GC(1) Pause Remark 0M->0M(8M) 1.325ms
[0.175s][info][gc,cpu       ] GC(1) User=0.00s Sys=0.00s Real=0.00s
[0.175s][info][gc,marking   ] GC(1) Concurrent Rebuild Remembered Sets
[0.175s][info][gc,marking   ] GC(1) Concurrent Rebuild Remembered Sets 0.076ms
[0.176s][info][gc,start     ] GC(1) Pause Cleanup
[0.176s][info][gc           ] GC(1) Pause Cleanup 0M->0M(8M) 0.047ms
[0.176s][info][gc,cpu       ] GC(1) User=0.00s Sys=0.00s Real=0.00s
[0.176s][info][gc,marking   ] GC(1) Concurrent Cleanup for Next Mark
[0.176s][info][gc,marking   ] GC(1) Concurrent Cleanup for Next Mark 0.113ms
[0.176s][info][gc           ] GC(1) Concurrent Cycle 3.745ms
[0.177s][info][gc,heap,exit ] Heap
[0.177s][info][gc,heap,exit ]  garbage-first heap   total 8192K, used 932K [0x0000000081c00000, 0x0000000100000000)
[0.177s][info][gc,heap,exit ]   region size 1024K, 2 young (2048K), 1 survivors (1024K)
[0.177s][info][gc,heap,exit ]  Metaspace       used 608K, capacity 4531K, committed 4864K, reserved 1056768K
[0.177s][info][gc,heap,exit ]   class space    used 56K, capacity 402K, committed 512K, reserved 1048576K

Note: After using the parameter -XX: + ExplicitGCInvokesConcurrent, G1 uses concurrent collection

 

 

**********************************

Disable System.gc (): The garbage collector will automatically determine the garbage collection. The available parameter -XX: + DisableExplicitGC disables System.gc ()

 

public class Test {

    public static void main(String[] args){
        System.gc();
    }
}

 

********************

Console output

 

Virtual machine parameters: -Xlog: gc * -XX: + DisableExplicitGC

[0.024s][info][gc,heap] Heap region size: 1M
[0.024s][info][gc,heap,coops] Heap address: 0x0000000081c00000, size: 2020 MB, Compressed Oops mode: 32-bit
[0.028s][info][gc           ] Using G1
[0.054s][info][gc           ] Periodic GC disabled
[0.159s][info][gc,heap,exit ] Heap
[0.159s][info][gc,heap,exit ]  garbage-first heap   total 131072K, used 2048K [0x0000000081c00000, 0x0000000100000000)
[0.159s][info][gc,heap,exit ]   region size 1024K, 3 young (3072K), 0 survivors (0K)
[0.159s][info][gc,heap,exit ]  Metaspace       used 610K, capacity 4531K, committed 4864K, reserved 1056768K
[0.159s][info][gc,heap,exit ]   class space    used 55K, capacity 402K, committed 512K, reserved 1048576K

Explanation: System.gc () did not trigger full gc

 

 

Published 387 original articles · Like 98 · Visits 30,000+

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/105203666