GC實例及參考(1)

  • java代碼:

    
    import java.lang.ref.WeakReference;
    /**
    * Created by niewj on 2017/12/17.
    */
    public class Main {
    public static void main(String[] args) {
        User a = new User();
        WeakReference<User> b = new WeakReference<User>(a);
    
        int i = 0;
        while (true) {
            if (b.get() != null) {
                i++;
                System.out.println("A還沒被回收!!" + i);
            } else {
                System.out.println("A---被回收了----------");
                break;
            }
        }
    }
    }

class User {
}


![不得不借用此神圖了](https://s1.51cto.com/images/blog/201803/27/c4cbef71ef123bf62d1bfde4344a9ae9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

---
#####1. UseSerialGC 
######_(UseSerialGC > Serial + Serial Old)_
`XX:+UseSerialGC -XX:+PrintGC -XX:+PrintGCDetails`
>......
A還沒被回收!!384384
A還沒被回收!!384385
A還沒被回收!!384386
[GC[_**DefNew**_: 68416K->812K(76928K), 0.0032924 secs] 68416K->812K(247872K), 0.0033418 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
A還沒被回收!!384387
A---被回收了----------
Heap
 **def new generation**   total 76928K, used 2837K [0x0000000700800000, 0x0000000705b70000, 0x0000000753f50000)
  _**eden space**_ 68416K,   2% used [0x0000000700800000, 0x00000007009fa518, 0x0000000704ad0000)
  _**from space**_ 8512K,   9% used [0x0000000705320000, 0x00000007053eb178, 0x0000000705b70000)
  _**to   space**_ 8512K,   0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
 **tenured generation**   total 170944K, used 0K [0x0000000753f50000, 0x000000075e640000, 0x00000007fae00000)
   the space 170944K,   0% used [0x0000000753f50000, 0x0000000753f50000, 0x0000000753f50200, 0x000000075e640000)
 **compacting perm gen**  total 21248K, used 3165K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
   the space 21248K,  14% used [0x00000007fae00000, 0x00000007fb117430, 0x00000007fb117600, 0x00000007fc2c0000)
No shared spaces configured.

#####2. UseParNew    
###### _(UseParNew > ParNew  +  Serial Old)_
`-XX:+UseParNewGC -XX:+PrintGC -XX:+PrintGCDetails`
> ......
A還沒被回收!!384386
[GC[_**ParNew**_: 68416K->837K(76928K), 0.0011650 secs] 68416K->837K(247872K), 0.0012095 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
A還沒被回收!!384387
A---被回收了----------
Heap
 **par new generation**   total 76928K, used 3560K [0x0000000700800000, 0x0000000705b70000, 0x0000000753f50000)
  _**eden space**_ 68416K,   3% used [0x0000000700800000, 0x0000000700aa8cd8, 0x0000000704ad0000)
  _**from space**_ 8512K,   9% used [0x0000000705320000, 0x00000007053f1540, 0x0000000705b70000)
  _**to   space**_ 8512K,   0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
 **tenured generation**   total 170944K, used 0K [0x0000000753f50000, 0x000000075e640000, 0x00000007fae00000)
   the space 170944K,   0% used [0x0000000753f50000, 0x0000000753f50000, 0x0000000753f50200, 0x000000075e640000)
 **compacting perm gen**  total 21248K, used 3165K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)
   the space 21248K,  14% used [0x00000007fae00000, 0x00000007fb117430, 0x00000007fb117600, 0x00000007fc2c0000)
No shared spaces configured.

#####3. UseConcMarkSweepGC   
###### _(UseConcMarkSweepGC > ParNew + CMS + ..Serial Old)_
`-XX:+UseConcMarkSweepGC -XX:+PrintGC -XX:+PrintGCDetails`
如果出現Concurrent Mode Failure, 老年代用 _**Serial Old 後備**_
`-XX:+UseConcMarkSweepGC -XX:+PrintGC -XX:+PrintGCDetails`
>  ......
A還沒被回收!!384385
A還沒被回收!!384386    
[GC[_**ParNew**_: 68416K->836K(76928K), 0.0013431 secs] 68416K->836K(247872K), 0.0014041 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
A還沒被回收!!384387
A---被回收了----------
Heap    
**par new generation**   total 76928K, used 3559K [0x0000000700800000, 0x0000000705b70000, 0x00000007154c0000)
  _**eden space**_ 68416K,   3% used [0x0000000700800000, 0x0000000700aa8cd8, 0x0000000704ad0000)
  _**from space**_ 8512K,   9% used [0x0000000705320000, 0x00000007053f1130, 0x0000000705b70000)
  _**to   space**_ 8512K,   0% used [0x0000000704ad0000, 0x0000000704ad0000, 0x0000000705320000)
 **concurrent mark-sweep generation** total 170944K, used 0K [0x00000007154c0000, 0x000000071fbb0000, 0x00000007fae00000)
 **concurrent-mark-sweep perm gen** total 21248K, used 3166K [0x00000007fae00000, 0x00000007fc2c0000, 0x0000000800000000)

##### 4. UseParallelGC 
###### _(UseParallelGC > Parallel Scanvenge + Serial Old)_
`-XX:+UseParallelGC -XX:+PrintGC -XX:+PrintGCDetails`
>A還沒被回收!!364573
A還沒被回收!!364574
[GC [_**PSYoungGen**_: 65024K->962K(75264K)] 65024K->970K(245760K), 0.0012285 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
A還沒被回收!!364575
A---被回收了----------
Heap
 **PSYoungGen**      total 75264K, used 3551K [0x00000007ac880000, 0x00000007b1c00000, 0x0000000800000000)
  _**eden space**_ 65024K, 3% used [0x00000007ac880000,0x00000007acb070d8,0x00000007b0800000)
  _**from space**_ 10240K, 9% used [0x00000007b0800000,0x00000007b08f0bd0,0x00000007b1200000)
  _**to   space**_ 10240K, 0% used [0x00000007b1200000,0x00000007b1200000,0x00000007b1c00000)
 **ParOldGen**       total 170496K, used 8K [0x0000000705a00000, 0x0000000710080000, 0x00000007ac880000)
  object space 170496K, 0% used [0x0000000705a00000,0x0000000705a02000,0x0000000710080000)
 **PSPermGen**       total 21504K, used 3165K [0x0000000700800000, 0x0000000701d00000, 0x0000000705a00000)
  object space 21504K, 14% used [0x0000000700800000,0x0000000700b17430,0x0000000701d00000)

##### 5. UseParallelOldGC 
###### _(UseParallelOldGC > Parallel Scavenge + Parallel Old)_
`-XX:+UseParallelOldGC -XX:+PrintGC -XX:+PrintGCDetails`
> ......
A還沒被回收!!364574
A還沒被回收!!364575
[GC [_**PSYoungGen**_: 65024K->874K(75264K)] 65024K->882K(245760K), 0.0020625 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
A還沒被回收!!364576
A---被回收了----------
Heap
> **PSYoungGen**      total 75264K, used 3463K [0x00000007ac880000, 0x00000007b1c00000, 0x0000000800000000)
  _**eden space**_ 65024K, 3% used [0x00000007ac880000,0x00000007acb070d8,0x00000007b0800000)
  _**from space**_ 10240K, 8% used [0x00000007b0800000,0x00000007b08dabc0,0x00000007b1200000)
  _**to   space**_ 10240K, 0% used [0x00000007b1200000,0x00000007b1200000,0x00000007b1c00000)
 **ParOldGen**       total 170496K, used 8K [0x0000000705a00000, 0x0000000710080000, 0x00000007ac880000)
  object space 170496K, 0% used [0x0000000705a00000,0x0000000705a02000,0x0000000710080000)
 **PSPermGen**       total 21504K, used 3165K [0x0000000700800000, 0x0000000701d00000, 0x0000000705a00000)
  object space 21504K, 14% used [0x0000000700800000,0x0000000700b17430,0x0000000701d00000)

##### 6. UseG1GC
###### UseG1GC
`-XX:+UseG1GC -XX:+PrintGC -XX:+PrintGCDetails`
>> ......
A還沒被回收!!71007
A還沒被回收!!71008
A還沒被回收!!71009[GC pause (young), 0.0012627 secs]
   [Parallel Time: 1.0 ms, GC Workers: 4]
      [GC Worker Start (ms): Min: 922.7, Avg: 922.8, Max: 922.8, Diff: 0.0]
      [Ext Root Scanning (ms): Min: 0.4, Avg: 0.4, Max: 0.5, Diff: 0.1, Sum: 1.8]
      [Update RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
         [Processed Buffers: Min: 0, Avg: 2.8, Max: 11, Diff: 11, Sum: 11]
      [Scan RS (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Code Root Scanning (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [Object Copy (ms): Min: 0.5, Avg: 0.5, Max: 0.5, Diff: 0.1, Sum: 2.0]
      [Termination (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.0]
      [GC Worker Other (ms): Min: 0.0, Avg: 0.0, Max: 0.0, Diff: 0.0, Sum: 0.1]
      [GC Worker Total (ms): Min: 1.0, Avg: 1.0, Max: 1.0, Diff: 0.0, Sum: 3.9]
      [GC Worker End (ms): Min: 923.7, Avg: 923.7, Max: 923.7, Diff: 0.0]
   [Code Root Fixup: 0.0 ms]
   [Code Root Migration: 0.0 ms]
   [Clear CT: 0.0 ms]
   [Other: 0.2 ms]
      [Choose CSet: 0.0 ms]
      [Ref Proc: 0.1 ms]
      [Ref Enq: 0.0 ms]
      [Free CSet: 0.0 ms]
   [**Eden**: 14.0M(14.0M)->0.0B(20.0M) **Survivors**: 0.0B->1024.0K **Heap**: 14.0M(251.0M)->896.1K(251.0M)]
 [Times: user=0.00 sys=0.00, real=0.00 secs]     
>
>>A---被回收了----------
Heap
 **garbage-first heap**   total 257024K, used 896K [0x0000000700800000, 0x0000000710300000, 0x00000007fae00000)
  **region size** 1024K, 2 young (2048K), 1 survivors (1024K)
 **compacting perm gen**  total 20480K, used 3164K [0x00000007fae00000, 0x00000007fc200000, 0x0000000800000000)
   the space 20480K,  15% used [0x00000007fae00000, 0x00000007fb117388, 0x00000007fb117400, 0x00000007fc200000)
No shared spaces configured.

相關文章課關聯參考
[Java GC系列(1):Java垃圾回收簡介](http://www.importnew.com/13504.html)
[Java GC系列(2):Java垃圾回收是如何工作的?](http://www.importnew.com/13493.html)
[Java GC系列(3):垃圾回收器種類](http://www.importnew.com/13827.html)
[Java GC系列(4):垃圾回收監視和分析](http://www.importnew.com/13838.html)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章