收集器簡介
https://blogs.oracle.com/jonthecollector/our-collectors
收集器 | 串行、並行or併發 | 新生代/老年代 | 算法 | 目標 | 適用場景 | 權威解釋 |
---|---|---|---|---|---|---|
Serial | 串行 | 新生代 | 複製算法 | 響應速度優先 | 單CPU環境下的Client模式 | "Serial" is a stop-the-world, copying collector which uses a single GC thread. |
Serial Old | 串行 | 老年代 | 標記-整理 | 響應速度優先 | 單CPU環境下的Client模式、CMS的後備預案 | "Serial Old" is a stop-the-world,mark-sweep-compact collector that uses a single GC thread. |
ParNew | 並行 | 新生代 | 複製算法 | 響應速度優先 | 多CPU環境時在Server模式下與CMS配合 | "ParNew" is a stop-the-world, copying collector which uses multiple GC threads. It differs from "Parallel Scavenge" in that it has enhancements that make it usable with CMS. For example, "ParNew" does the synchronization needed so that it can run during the concurrent phases of CMS. |
Parallel Scavenge | 並行 | 新生代 | 複製算法 | 吞吐量優先 | 在後臺運算而不需要太多交互的任務 | "Parallel Scavenge" is a stop-the-world, copying collector which uses multiple GC threads. |
Parallel Old | 並行 | 老年代 | 標記-整理 | 吞吐量優先 | 在後臺運算而不需要太多交互的任務 | "Parallel Old" is a compacting collector that uses multiple GC threads.Using the -XX flags for our collectors for jdk6 |
CMS | 併發 | 老年代 | 標記-清除 | 響應速度優先 | 集中在互聯網站或B/S系統服務端上的Java應用 | "CMS" is a mostly concurrent, low-pause collector |
G1 | 併發 | both | 標記-整理+複製算法 | 響應速度優先 | 面向服務端應用,將來替換CMS |
JVM 對應選項的回收器組合
- UseSerialGC is "Serial" + "Serial Old"
- UseParNewGC is "ParNew" + "Serial Old"
- UseConcMarkSweepGC is
"ParNew" + "CMS" + "Serial Old". "CMS"
is used most of the time to collect the tenured generation. "Serial Old" is used when a concurrent mode failure occurs.
why
https://stackoverflow.com/questions/39569649/why-is-cms-full-gc-single-threaded?from=timeline&isappinstalled=0 簡單翻譯一下:因爲開發人員的精力都在G1上,而且 CMS發現FULL GC的情況很少,另外每個回收器的內存實現很複雜,不能簡單地進行兼容,需要開發。所以沒有支持para old,但已經提了patch,只不過沒有快實現 - UseParallelGC is "Parallel Scavenge" + "Serial Old"
- UseParallelOldGC is "Parallel Scavenge" + "Parallel Old"
JDK版本默認垃圾收集器
jdk1.7 默認垃圾收集器Parallel Scavenge(新生代)+Serial Old(老年代)
jdk1.8 默認垃圾收集器Parallel Scavenge(新生代)+Serial Old(老年代)
jdk1.9 默認垃圾收集器G1
jdk10 默認垃圾收集器G1
-XX:+PrintCommandLineFlags jvm參數可查看默認設置收集器類型
java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=132435328 -XX:MaxHeapSize=2118965248 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
常用的收集器組合
https://blogs.oracle.com/jonthecollector/our-collectors
再談引用
小哥的公司和正常的公司發展沒有多大不一樣,客戶關係越來越複雜,有的客戶很不規矩,經常把人介紹進來,然後忘了帶走,掃地阿姨不敢給趕走,因爲不確定這哥們會不會還有用。時間一長,這種客戶越來越多,辦公區域又不夠了。這種情況有兩種,一種是VIP區域中沒有關係的用戶,這種用戶當VIP區域的掃地阿姨發現空間不足時,會被清掉!但如果這種用戶太多,也會增阿姨的負責,阿姨清掃的時間長,影響客戶體驗。
第二種是到VIP區域後依賴有關係的用戶,老闆的親戚。解決這種問題的辦法從根源上杜絕走後門。比如ThreadLocal 的remove()方法。
深入thread local 本質
https://www.jianshu.com/p/fca69ba27de6