"/>

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?/h1>

Posted 娣卞叆鍘熺悊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?/h1>

相关的知识,希望对你有一定的参考价值。

鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>

鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>

鍓嶈█
Garbage First(G1)鏄瀮鍦炬敹闆嗛鍩熺殑鏈€鏂版垚鏋滐紝鍚屾椂涔熸槸HotSpot鍦↗VM涓婂姏鎺ㄧ殑鍨冨溇鏀堕泦鍣紝骞惰祴浜堝彇浠MS鐨勪娇鍛姐€傚鏋滀娇鐢↗ava 8/9锛岄偅涔堟湁寰堝ぇ鍙兘甯屾湜瀵笹1鏀堕泦鍣ㄨ繘琛岃瘎浼般€傛湰鏂囪缁嗛鍏堝JVM鍏朵粬鐨勫瀮鍦炬敹闆嗗櫒杩涜鎬荤粨锛屽苟涓嶨1杩涜浜嗙畝鍗曠殑瀵规瘮锛涚劧鍚庨€氳繃G1鐨勫唴瀛樻ā鍨嬨€丟1鐨勬椿鍔ㄥ懆鏈燂紝瀵笹1鐨勫伐浣滄満鍒惰繘琛屼簡浠嬬粛锛涘悓鏃惰繕鍦ㄤ粙缁嶈繃绋嬩腑锛屾弿杩颁簡鍙兘闇€瑕佸紩璧锋敞鎰忕殑浼樺寲鐐广€傜瑪鑰呭笇鏈涢€氳繃鏈枃锛岃鏈変竴瀹欽VM鍩虹鐨勮鑰呰兘灏藉揩鎺屾彙G1鐨勭煡璇嗙偣銆?/p>

绗竴绔?姒傝堪

G1(Garbage First)鍨冨溇鏀堕泦鍣ㄦ槸褰撲粖鍨冨溇鍥炴敹鎶€鏈渶鍓嶆部鐨勬垚鏋滀箣涓€銆傛棭鍦↗DK7灏卞凡鍔犲叆JVM鐨勬敹闆嗗櫒澶у搴腑锛屾垚涓篐otSpot閲嶇偣鍙戝睍鐨勫瀮鍦惧洖鏀舵妧鏈€傚悓浼樼鐨凜MS鍨冨溇鍥炴敹鍣ㄤ竴鏍凤紝G1涔熸槸鍏虫敞鏈€灏忔椂寤剁殑鍨冨溇鍥炴敹鍣紝涔熷悓鏍烽€傚悎澶у昂瀵稿爢鍐呭瓨鐨勫瀮鍦炬敹闆嗭紝瀹樻柟涔熸帹鑽愪娇鐢℅1鏉ヤ唬鏇块€夋嫨CMS銆侴1鏈€澶х殑鐗圭偣鏄紩鍏ュ垎鍖虹殑鎬濊矾锛屽急鍖栦簡鍒嗕唬鐨勬蹇碉紝鍚堢悊鍒╃敤鍨冨溇鏀堕泦鍚勪釜鍛ㄦ湡鐨勮祫婧愶紝瑙e喅浜嗗叾浠栨敹闆嗗櫒鐢氳嚦CMS鐨勪紬澶氱己闄枫€?/p>

绗簩绔?JVM GC鏀堕泦鍣ㄧ殑鍥為【涓庢瘮杈?/h1>

  1. 鍥句腑灞曠ず浜?绉嶄笉鍚屽垎浠g殑鏀堕泦鍣細

    Serial銆丳arNew銆丳arallel Scavenge銆丼erial Old銆丳arallel Old銆丆MS銆丟1锛?/p>

  2. 鑰屽畠浠墍澶勫尯鍩燂紝鍒欒〃鏄庡叾鏄睘浜庢柊鐢熶唬鏀堕泦鍣ㄨ繕鏄€佸勾浠f敹闆嗗櫒锛?/p>

    鏂扮敓浠f敹闆嗗櫒锛歋erial銆丳arNew銆丳arallel Scavenge锛?/p>

    鑰佸勾浠f敹闆嗗櫒锛歋erial Old銆丳arallel Old銆丆MS锛?/p>

    鏁村爢鏀堕泦鍣細G1锛?/p>

  3. 涓や釜鏀堕泦鍣ㄩ棿鏈夎繛绾匡紝琛ㄦ槑瀹冧滑鍙互鎼厤浣跨敤锛?/p>

    Serial/Serial Old銆丼erial/CMS銆丳arNew/Serial Old銆丳arNew/CMS銆丳arallel Scavenge/Serial Old銆丳arallel Scavenge/Parallel Old銆丟1锛?/p>

涓茶鏀堕泦鍣?/h2>

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=涓茶鏀堕泦鍣ㄧ粍鍚?Serial + Serial Old

寮€鍚€夐」锛?code class="mq-43">-XX:+SerialGC

涓茶鏀堕泦鍣ㄦ槸鏈€鍩烘湰銆佸彂灞曟椂闂存渶闀裤€佷箙缁忚€冮獙鐨勫瀮鍦炬敹闆嗗櫒锛屼篃鏄痗lient妯″紡涓嬬殑榛樿鏀堕泦鍣ㄩ厤缃€?/p>

涓茶鏀堕泦鍣ㄩ噰鐢ㄥ崟绾跨▼stop-the-world鐨勬柟寮忚繘琛屾敹闆嗐€傚綋鍐呭瓨涓嶈冻鏃讹紝涓茶GC璁剧疆鍋滈】鏍囪瘑锛屽緟鎵€鏈夌嚎绋嬮兘杩涘叆瀹夊叏鐐?Safepoint)鏃讹紝搴旂敤绾跨▼鏆傚仠锛屼覆琛孏C寮€濮嬪伐浣滐紝閲囩敤鍗曠嚎绋嬫柟寮忓洖鏀剁┖闂村苟鏁寸悊鍐呭瓨銆傚崟绾跨▼涔熸剰鍛崇潃澶嶆潅搴︽洿浣庛€佸崰鐢ㄥ唴瀛樻洿灏戯紝浣嗗悓鏃朵篃鎰忓懗鐫€涓嶈兘鏈夋晥鍒╃敤澶氭牳浼樺娍銆備簨瀹炰笂锛屼覆琛屾敹闆嗗櫒鐗瑰埆閫傚悎鍫嗗唴瀛樹笉楂樸€佸崟鏍哥敋鑷冲弻鏍窩PU鐨勫満鍚堛€?/p>

骞惰鏀堕泦鍣?/h2>

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣? class=

骞惰鏀堕泦鍣ㄧ粍鍚?Parallel Scavenge + Parallel Old

寮€鍚€夐」锛?code class="mq-51">-XX:+UseParallelGC鎴?code class="mq-52">-XX:+UseParallelOldGC(鍙簰鐩告縺娲?

骞惰鏀堕泦鍣ㄦ槸浠ュ叧娉ㄥ悶鍚愰噺涓虹洰鏍囩殑鍨冨溇鏀堕泦鍣紝涔熸槸server妯″紡涓嬬殑榛樿鏀堕泦鍣ㄩ厤缃紝瀵瑰悶鍚愰噺鐨勫叧娉ㄤ富瑕佷綋鐜板湪骞磋交浠arallel Scavenge鏀堕泦鍣ㄤ笂銆?/p>

骞惰鏀堕泦鍣ㄤ笌涓茶鏀堕泦鍣ㄥ伐浣滄ā寮忕浉浼硷紝閮芥槸stop-the-world鏂瑰紡锛屽彧鏄殏鍋滄椂骞惰鍦拌繘琛屽瀮鍦炬敹闆嗐€傚勾杞讳唬閲囩敤澶嶅埗绠楁硶锛岃€佸勾浠i噰鐢ㄦ爣璁?鏁寸悊锛屽湪鍥炴敹鐨勫悓鏃惰繕浼氬鍐呭瓨杩涜鍘嬬缉銆傚叧娉ㄥ悶鍚愰噺涓昏鎸囧勾杞讳唬鐨凱arallel Scavenge鏀堕泦鍣紝閫氳繃涓や釜鐩爣鍙傛暟-XX:MaxGCPauseMills鍜?code class="mq-56">-XX:GCTimeRatio锛岃皟鏁存柊鐢熶唬绌洪棿澶у皬锛屾潵闄嶄綆GC瑙﹀彂鐨勯鐜囥€傚苟琛屾敹闆嗗櫒閫傚悎瀵瑰悶鍚愰噺瑕佹眰杩滆繙楂樹簬寤惰繜瑕佹眰鐨勫満鏅紝骞朵笖鍦ㄦ弧瓒虫渶宸欢鏃剁殑鎯呭喌涓嬶紝骞惰鏀堕泦鍣ㄥ皢鎻愪緵鏈€浣崇殑鍚炲悙閲忋€?/p>

骞跺彂鏍囪娓呴櫎鏀堕泦鍣?/h2>

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=骞跺彂鏍囪娓呴櫎鏀堕泦鍣ㄧ粍鍚?ParNew + CMS + Serial Old

寮€鍚€夐」锛?code class="mq-61">-XX:+UseConcMarkSweepGC

骞跺彂鏍囪娓呴櫎(CMS)鏄互鍏虫敞寤惰繜涓虹洰鏍囥€佸崄鍒嗕紭绉€鐨勫瀮鍦惧洖鏀剁畻娉曪紝寮€鍚悗锛屽勾杞讳唬浣跨敤STW寮忕殑骞惰鏀堕泦锛岃€佸勾浠e洖鏀堕噰鐢–MS杩涜鍨冨溇鍥炴敹锛屽寤惰繜鐨勫叧娉ㄤ篃涓昏浣撶幇鍦ㄨ€佸勾浠MS涓娿€?/p>

骞磋交浠arNew涓庡苟琛屾敹闆嗗櫒绫讳技锛岃€岃€佸勾浠MS姣忎釜鏀堕泦鍛ㄦ湡閮借缁忓巻锛?span>鍒濆鏍囪銆佸苟鍙戞爣璁般€侀噸鏂版爣璁般€佸苟鍙戞竻闄?/strong>銆傚叾涓紝鍒濆鏍囪浠TW鐨勬柟寮忔爣璁版墍鏈夌殑鏍瑰璞★紱骞跺彂鏍囪鍒欏悓搴旂敤绾跨▼涓€璧峰苟琛岋紝鏍囪鍑烘牴瀵硅薄鐨勫彲杈捐矾寰勶紱鍦ㄨ繘琛屽瀮鍦惧洖鏀跺墠锛孋MS鍐嶄互涓€涓猄TW杩涜閲嶆柊鏍囪锛屾爣璁伴偅浜涚敱mutator绾跨▼(鎸囧紩璧锋暟鎹彉鍖栫殑绾跨▼锛屽嵆搴旂敤绾跨▼)淇敼鑰屽彲鑳介敊杩囩殑鍙揪瀵硅薄锛涙渶鍚庡緱鍒扮殑涓嶅彲杈惧璞″皢鍦ㄥ苟鍙戞竻闄ら樁娈佃繘琛屽洖鏀躲€?span>鍊煎緱娉ㄦ剰鐨勬槸锛屽垵濮嬫爣璁板拰閲嶆柊鏍囪閮藉凡浼樺寲涓哄绾跨▼鎵ц銆侰MS闈炲父閫傚悎鍫嗗唴瀛樺ぇ銆丆PU鏍告暟澶氱殑鏈嶅姟鍣ㄧ搴旂敤锛屼篃鏄疓1鍑虹幇涔嬪墠澶у瀷搴旂敤鐨勯閫夋敹闆嗗櫒銆?/p>

浣嗘槸CMS骞朵笉瀹岀編锛屽畠鏈変互涓嬬己鐐癸細

  1. 鐢变簬骞跺彂杩涜锛孋MS鍦ㄦ敹闆嗕笌搴旂敤绾跨▼浼氬悓鏃朵細澧炲姞瀵瑰爢鍐呭瓨鐨勫崰鐢紝涔熷氨鏄锛孋MS蹇呴』瑕佸湪鑰佸勾浠e爢鍐呭瓨鐢ㄥ敖涔嬪墠瀹屾垚鍨冨溇鍥炴敹锛屽惁鍒機MS鍥炴敹澶辫触鏃讹紝灏嗚Е鍙戞媴淇濇満鍒讹紝涓茶鑰佸勾浠f敹闆嗗櫒灏嗕細浠TW鐨勬柟寮忚繘琛屼竴娆C锛屼粠鑰岄€犳垚杈冨ぇ鍋滈】鏃堕棿锛?/p>

  2. 鏍囪娓呴櫎绠楁硶鏃犳硶鏁寸悊绌洪棿纰庣墖锛岃€佸勾浠g┖闂翠細闅忕潃搴旂敤鏃堕暱琚€愭鑰楀敖锛屾渶鍚庡皢涓嶅緱涓嶉€氳繃鎷呬繚鏈哄埗瀵瑰爢鍐呭瓨杩涜鍘嬬缉銆侰MS涔熸彁渚涗簡鍙傛暟-XX:CMSFullGCsBeForeCompaction(榛樿0锛屽嵆姣忔閮借繘琛屽唴瀛樻暣鐞?鏉ユ寚瀹氬灏戞CMS鏀堕泦涔嬪悗锛岃繘琛屼竴娆″帇缂╃殑Full GC銆?/p>

Garbage First

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=Garbage First (G1)

寮€鍚€夐」锛?code class="mq-72">-XX:+UseG1GC

涔嬪墠浠嬬粛鐨勫嚑缁勫瀮鍦炬敹闆嗗櫒缁勫悎锛岄兘鏈夊嚑涓叡鍚岀偣锛?/p>

  1. 骞磋交浠c€佽€佸勾浠f槸鐙珛涓旇繛缁殑鍐呭瓨鍧楋紱

  2. 骞磋交浠f敹闆嗕娇鐢ㄥ崟eden銆佸弻survivor杩涜澶嶅埗绠楁硶锛?/p>

  3. 鑰佸勾浠f敹闆嗗繀椤绘壂鎻忔暣涓€佸勾浠e尯鍩燂紱

  4. 閮芥槸浠ュ敖鍙兘灏戣€屽潡鍦版墽琛孏C涓鸿璁″師鍒欍€?/p>

G1鍨冨溇鏀堕泦鍣ㄤ篃鏄互鍏虫敞寤惰繜涓虹洰鏍囥€佹湇鍔″櫒绔簲鐢ㄧ殑鍨冨溇鏀堕泦鍣紝琚獺otSpot鍥㈤槦瀵勪簣鍙栦唬CMS鐨勪娇鍛斤紝涔熸槸涓€涓潪甯稿叿鏈夎皟浼樻綔鍔涚殑鍨冨溇鏀堕泦鍣ㄣ€傝櫧鐒禛1涔熸湁绫讳技CMS鐨勬敹闆嗗姩浣滐細鍒濆鏍囪銆佸苟鍙戞爣璁般€侀噸鏂版爣璁般€佹竻闄ゃ€佽浆绉诲洖鏀讹紝骞朵笖涔熶互涓€涓覆琛屾敹闆嗗櫒鍋氭媴淇濇満鍒讹紝浣嗗崟绾湴浠ョ被浼煎墠涓夌鐨勮繃绋嬫弿杩版樉寰楀苟涓嶆槸寰堝Ε褰撱€備簨瀹炰笂锛孏1鏀堕泦涓庝互涓婁笁缁勬敹闆嗗櫒鏈夊緢澶т笉鍚岋細

  1. G1鐨勮璁″師鍒欐槸"棣栧厛鏀堕泦灏藉彲鑳藉鐨勫瀮鍦?Garbage First)"銆傚洜姝わ紝G1骞朵笉浼氱瓑鍐呭瓨鑰楀敖(涓茶銆佸苟琛?鎴栬€呭揩鑰楀敖(CMS)鐨勬椂鍊欏紑濮嬪瀮鍦炬敹闆嗭紝鑰屾槸鍦ㄥ唴閮ㄩ噰鐢ㄤ簡鍚彂寮忕畻娉曪紝鍦ㄨ€佸勾浠f壘鍑哄叿鏈夐珮鏀堕泦鏀剁泭鐨勫垎鍖鸿繘琛屾敹闆嗐€傚悓鏃禛1鍙互鏍规嵁鐢ㄦ埛璁剧疆鐨勬殏鍋滄椂闂寸洰鏍囪嚜鍔ㄨ皟鏁村勾杞讳唬鍜屾€诲爢澶у皬锛屾殏鍋滅洰鏍囪秺鐭勾杞讳唬绌洪棿瓒婂皬銆佹€荤┖闂村氨瓒婂ぇ锛?/p>

  2. G1閲囩敤鍐呭瓨鍒嗗尯(Region)鐨勬€濊矾锛屽皢鍐呭瓨鍒掑垎涓轰竴涓釜鐩哥瓑澶у皬鐨勫唴瀛樺垎鍖猴紝鍥炴敹鏃跺垯浠ュ垎鍖轰负鍗曚綅杩涜鍥炴敹锛屽瓨娲荤殑瀵硅薄澶嶅埗鍒板彟涓€涓┖闂插垎鍖轰腑銆傜敱浜庨兘鏄互鐩哥瓑澶у皬鐨勫垎鍖轰负鍗曚綅杩涜鎿嶄綔锛屽洜姝1澶╃劧灏辨槸涓€绉嶅帇缂╂柟妗?灞€閮ㄥ帇缂?锛?/p>

  3. G1铏界劧涔熸槸鍒嗕唬鏀堕泦鍣紝浣嗘暣涓唴瀛樺垎鍖轰笉瀛樺湪鐗╃悊涓婄殑骞磋交浠d笌鑰佸勾浠g殑鍖哄埆锛屼篃涓嶉渶瑕佸畬鍏ㄧ嫭绔嬬殑survivor(to space)鍫嗗仛澶嶅埗鍑嗗銆侴1鍙湁閫昏緫涓婄殑鍒嗕唬姒傚康锛屾垨鑰呰姣忎釜鍒嗗尯閮藉彲鑳介殢G1鐨勮繍琛屽湪涓嶅悓浠d箣闂村墠鍚庡垏鎹紱

  4. G1鐨勬敹闆嗛兘鏄疭TW鐨勶紝浣嗗勾杞讳唬鍜岃€佸勾浠g殑鏀堕泦鐣岄檺姣旇緝妯$硦锛岄噰鐢ㄤ簡娣峰悎(mixed)鏀堕泦鐨勬柟寮忋€傚嵆姣忔鏀堕泦鏃㈠彲鑳藉彧鏀堕泦骞磋交浠e垎鍖?骞磋交浠f敹闆?锛屼篃鍙兘鍦ㄦ敹闆嗗勾杞讳唬鐨勫悓鏃讹紝鍖呭惈閮ㄥ垎鑰佸勾浠e垎鍖?娣峰悎鏀堕泦)锛岃繖鏍峰嵆浣垮爢鍐呭瓨寰堝ぇ鏃讹紝涔熷彲浠ラ檺鍒舵敹闆嗚寖鍥达紝浠庤€岄檷浣庡仠椤裤€?/p>

绗笁绔?G1鐨勫唴瀛樻ā鍨?/h1>

鍒嗗尯姒傚康

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<h3 class=鍒嗗尯

鍒嗗尯 Region

G1閲囩敤浜嗗垎鍖?Region)鐨勬€濊矾锛屽皢鏁翠釜鍫嗙┖闂村垎鎴愯嫢骞蹭釜澶у皬鐩哥瓑鐨勫唴瀛樺尯鍩燂紝姣忔鍒嗛厤瀵硅薄绌洪棿灏嗛€愭鍦颁娇鐢ㄥ唴瀛樸€傚洜姝わ紝鍦ㄥ爢鐨勪娇鐢ㄤ笂锛孏1骞朵笉瑕佹眰瀵硅薄鐨勫瓨鍌ㄤ竴瀹氭槸鐗╃悊涓婅繛缁殑锛屽彧瑕侀€昏緫涓婅繛缁嵆鍙紱姣忎釜鍒嗗尯涔熶笉浼氱‘瀹氬湴涓烘煇涓唬鏈嶅姟锛屽彲浠ユ寜闇€鍦ㄥ勾杞讳唬鍜岃€佸勾浠d箣闂村垏鎹€傚惎鍔ㄦ椂鍙互閫氳繃鍙傛暟-XX:G1HeapRegionSize=n鍙寚瀹氬垎鍖哄ぇ灏?1MB~32MB锛屼笖蹇呴』鏄?鐨勫箓)锛岄粯璁ゅ皢鏁村爢鍒掑垎涓?048涓垎鍖恒€?/p>

鍗$墖

鍗$墖 Card

鍦ㄦ瘡涓垎鍖哄唴閮ㄥ張琚垎鎴愪簡鑻ュ共涓ぇ灏忎负512 Byte鍗$墖(Card)锛屾爣璇嗗爢鍐呭瓨鏈€灏忓彲鐢ㄧ矑搴︽墍鏈夊垎鍖虹殑鍗$墖灏嗕細璁板綍鍦ㄥ叏灞€鍗$墖琛?Global Card Table)涓紝鍒嗛厤鐨勫璞′細鍗犵敤鐗╃悊涓婅繛缁殑鑻ュ共涓崱鐗囷紝褰撴煡鎵惧鍒嗗尯鍐呭璞$殑寮曠敤鏃朵究鍙€氳繃璁板綍鍗$墖鏉ユ煡鎵捐寮曠敤瀵硅薄(瑙丷Set)銆傛瘡娆″鍐呭瓨鐨勫洖鏀讹紝閮芥槸瀵规寚瀹氬垎鍖虹殑鍗$墖杩涜澶勭悊銆?/p>

鍫?/h3>

鍫?Heap

G1鍚屾牱鍙互閫氳繃-Xms/-Xmx鏉ユ寚瀹氬爢绌洪棿澶у皬銆傚綋鍙戠敓骞磋交浠f敹闆嗘垨娣峰悎鏀堕泦鏃讹紝閫氳繃璁$畻GC涓庡簲鐢ㄧ殑鑰楄垂鏃堕棿姣旓紝鑷姩璋冩暣鍫嗙┖闂村ぇ灏忋€傚鏋淕C棰戠巼澶珮锛屽垯閫氳繃澧炲姞鍫嗗昂瀵革紝鏉ュ噺灏慓C棰戠巼锛岀浉搴斿湴GC鍗犵敤鐨勬椂闂翠篃闅忎箣闄嶄綆锛涚洰鏍囧弬鏁?code class="mq-93">-XX:GCTimeRatio鍗充负GC涓庡簲鐢ㄧ殑鑰楄垂鏃堕棿姣旓紝G1榛樿涓?锛岃€孋MS榛樿涓?9锛屽洜涓篊MS鐨勮璁″師鍒欐槸鑰楄垂鍦℅C涓婄殑鏃堕棿灏藉彲鑳界殑灏戙€傚彟澶栵紝褰撶┖闂翠笉瓒筹紝濡傚璞$┖闂村垎閰嶆垨杞Щ澶辫触鏃讹紝G1浼氶鍏堝皾璇曞鍔犲爢绌洪棿锛屽鏋滄墿瀹瑰け璐ワ紝鍒欏彂璧锋媴淇濈殑Full GC銆侳ull GC鍚庯紝鍫嗗昂瀵歌绠楃粨鏋滀篃浼氳皟鏁村爢绌洪棿銆?/p>

鍒嗕唬妯″瀷

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<h3 class=鍒嗕唬

鍒嗕唬 Generation

鍒嗕唬鍨冨溇鏀堕泦鍙互灏嗗叧娉ㄧ偣闆嗕腑鍦ㄦ渶杩戣鍒嗛厤鐨勫璞′笂锛岃€屾棤闇€鏁村爢鎵弿锛岄伩鍏嶉暱鍛藉璞$殑鎷疯礉锛屽悓鏃剁嫭绔嬫敹闆嗘湁鍔╀簬闄嶄綆鍝嶅簲鏃堕棿銆傝櫧鐒跺垎鍖轰娇寰楀唴瀛樺垎閰嶄笉鍐嶈姹傜揣鍑戠殑鍐呭瓨绌洪棿锛屼絾G1渚濈劧浣跨敤浜嗗垎浠g殑鎬濇兂銆備笌鍏朵粬鍨冨溇鏀堕泦鍣ㄧ被浼硷紝G1灏嗗唴瀛樺湪閫昏緫涓婂垝鍒嗕负骞磋交浠e拰鑰佸勾浠o紝鍏朵腑骞磋交浠e張鍒掑垎涓篍den绌洪棿鍜孲urvivor绌洪棿銆備絾骞磋交浠g┖闂村苟涓嶆槸鍥哄畾涓嶅彉鐨勶紝褰撶幇鏈夊勾杞讳唬鍒嗗尯鍗犳弧鏃讹紝JVM浼氬垎閰嶆柊鐨勭┖闂插垎鍖哄姞鍏ュ埌骞磋交浠g┖闂淬€?/p>

鏁翠釜骞磋交浠e唴瀛樹細鍦ㄥ垵濮嬬┖闂?code class="mq-101">-XX:G1NewSizePercent(榛樿鏁村爢5%)涓庢渶澶х┖闂?code class="mq-102">-XX:G1MaxNewSizePercent(榛樿60%)涔嬮棿鍔ㄦ€佸彉鍖栵紝涓旂敱鍙傛暟鐩爣鏆傚仠鏃堕棿-XX:MaxGCPauseMillis(榛樿200ms)銆侀渶瑕佹墿缂╁鐨勫ぇ灏忎互鍙婂垎鍖虹殑宸茶蹇嗛泦鍚?RSet)璁$畻寰楀埌銆傚綋鐒讹紝G1渚濈劧鍙互璁剧疆鍥哄畾鐨勫勾杞讳唬澶у皬(鍙傛暟-XX:NewRatio銆?code class="mq-105">-Xmn)锛屼絾鍚屾椂鏆傚仠鐩爣灏嗗け鍘绘剰涔夈€?/p>

鏈湴鍒嗛厤缂撳啿

鏈湴鍒嗛厤缂撳啿 Local allocation buffer (Lab)

鍊煎緱娉ㄦ剰鐨勬槸锛岀敱浜庡垎鍖虹殑鎬濇兂锛屾瘡涓嚎绋嬪潎鍙互"璁ら"鏌愪釜鍒嗗尯鐢ㄤ簬绾跨▼鏈湴鐨勫唴瀛樺垎閰嶏紝鑰屼笉闇€瑕侀【鍙婂垎鍖烘槸鍚﹁繛缁€傚洜姝わ紝姣忎釜搴旂敤绾跨▼鍜孏C绾跨▼閮戒細鐙珛鐨勪娇鐢ㄥ垎鍖猴紝杩涜€屽噺灏戝悓姝ユ椂闂达紝鎻愬崌GC鏁堢巼锛岃繖涓垎鍖虹О涓烘湰鍦板垎閰嶇紦鍐插尯(Lab)銆?/p>

鍏朵腑锛屽簲鐢ㄧ嚎绋嬪彲浠ョ嫭鍗犱竴涓湰鍦扮紦鍐插尯(TLAB)鏉ュ垱寤虹殑瀵硅薄锛岃€屽ぇ閮ㄥ垎閮戒細钀藉叆Eden鍖哄煙(宸ㄥ瀷瀵硅薄鎴栧垎閰嶅け璐ラ櫎澶?锛屽洜姝LAB鐨勫垎鍖哄睘浜嶦den绌洪棿锛涜€屾瘡娆″瀮鍦炬敹闆嗘椂锛屾瘡涓狦C绾跨▼鍚屾牱鍙互鐙崰涓€涓湰鍦扮紦鍐插尯(GCLAB)鐢ㄦ潵杞Щ瀵硅薄锛屾瘡娆″洖鏀朵細灏嗗璞″鍒跺埌Suvivor绌洪棿鎴栬€佸勾浠g┖闂达紱瀵逛簬浠嶦den/Survivor绌洪棿鏅嬪崌(Promotion)鍒癝urvivor/鑰佸勾浠g┖闂寸殑瀵硅薄锛屽悓鏍锋湁GC鐙崰鐨勬湰鍦扮紦鍐插尯杩涜鎿嶄綔锛岃閮ㄥ垎绉颁负鏅嬪崌鏈湴缂撳啿鍖?PLAB)銆?/p>

鍒嗗尯妯″瀷

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=G1瀵瑰唴瀛樼殑浣跨敤浠ュ垎鍖?Region)涓哄崟浣嶏紝鑰屽瀵硅薄鐨勫垎閰嶅垯浠ュ崱鐗?Card)涓哄崟浣嶃€?br>

宸ㄥ瀷瀵硅薄

宸ㄥ瀷瀵硅薄 Humongous Region

涓€涓ぇ灏忚揪鍒扮敋鑷宠秴杩囧垎鍖哄ぇ灏忎竴鍗婄殑瀵硅薄绉颁负宸ㄥ瀷瀵硅薄(Humongous Object)銆傚綋绾跨▼涓哄法鍨嬪垎閰嶇┖闂存椂锛屼笉鑳界畝鍗曞湪TLAB杩涜鍒嗛厤锛屽洜涓哄法鍨嬪璞$殑绉诲姩鎴愭湰寰堥珮锛岃€屼笖鏈夊彲鑳戒竴涓垎鍖轰笉鑳藉绾冲法鍨嬪璞°€傚洜姝わ紝宸ㄥ瀷瀵硅薄浼氱洿鎺ュ湪鑰佸勾浠e垎閰嶏紝鎵€鍗犵敤鐨勮繛缁┖闂寸О涓哄法鍨嬪垎鍖?Humongous Region)銆侴1鍐呴儴鍋氫簡涓€涓紭鍖栵紝涓€鏃﹀彂鐜版病鏈夊紩鐢ㄦ寚鍚戝法鍨嬪璞★紝鍒欏彲鐩存帴鍦ㄥ勾杞讳唬鏀堕泦鍛ㄦ湡涓鍥炴敹銆?/p>

宸ㄥ瀷瀵硅薄浼氱嫭鍗犱竴涓€佹垨澶氫釜杩炵画鍒嗗尯锛屽叾涓涓€涓垎鍖鸿鏍囪涓哄紑濮嬪法鍨?StartsHumongous)锛岀浉閭昏繛缁垎鍖鸿鏍囪涓鸿繛缁法鍨?ContinuesHumongous)銆傜敱浜庢棤娉曚韩鍙桳ab甯︽潵鐨勪紭鍖栵紝骞朵笖纭畾涓€鐗囪繛缁殑鍐呭瓨绌洪棿闇€瑕佹壂鎻忔暣鍫嗭紝鍥犳纭畾宸ㄥ瀷瀵硅薄寮€濮嬩綅缃殑鎴愭湰闈炲父楂橈紝濡傛灉鍙互锛屽簲鐢ㄧ▼搴忓簲閬垮厤鐢熸垚宸ㄥ瀷瀵硅薄銆?/p>

宸茶蹇嗛泦鍚?/h3>

宸茶蹇嗛泦鍚?Remember Set (RSet)

鍦ㄤ覆琛屽拰骞惰鏀堕泦鍣ㄤ腑锛孏C閫氳繃鏁村爢鎵弿锛屾潵纭畾瀵硅薄鏄惁澶勪簬鍙揪璺緞涓€傜劧鑰孏1涓轰簡閬垮厤STW寮忕殑鏁村爢鎵弿锛屽湪姣忎釜鍒嗗尯璁板綍浜嗕竴涓凡璁板繂闆嗗悎(RSet)锛屽唴閮ㄧ被浼间竴涓弽鍚戞寚閽堬紝璁板綍寮曠敤鍒嗗尯鍐呭璞$殑鍗$墖绱㈠紩銆傚綋瑕佸洖鏀惰鍒嗗尯鏃讹紝閫氳繃鎵弿鍒嗗尯鐨凴Set锛屾潵纭畾寮曠敤鏈垎鍖哄唴鐨勫璞℃槸鍚﹀瓨娲伙紝杩涜€岀‘瀹氭湰鍒嗗尯鍐呯殑瀵硅薄瀛樻椿鎯呭喌銆?/p>

浜嬪疄涓婏紝骞堕潪鎵€鏈夌殑寮曠敤閮介渶瑕佽褰曞湪RSet涓紝濡傛灉涓€涓垎鍖虹‘瀹氶渶瑕佹壂鎻忥紝閭d箞鏃犻渶RSet涔熷彲浠ユ棤閬楁紡鐨勫緱鍒板紩鐢ㄥ叧绯汇€傞偅涔堝紩鐢ㄦ簮鑷湰鍒嗗尯鐨勫璞★紝褰撶劧涓嶇敤钀藉叆RSet涓紱鍚屾椂锛孏1 GC姣忔閮戒細瀵瑰勾杞讳唬杩涜鏁翠綋鏀堕泦锛屽洜姝ゅ紩鐢ㄦ簮鑷勾杞讳唬鐨勫璞★紝涔熶笉闇€瑕佸湪RSet涓褰曘€傛渶鍚庡彧鏈夎€佸勾浠g殑鍒嗗尯鍙兘浼氭湁RSet璁板綍锛岃繖浜涘垎鍖虹О涓烘嫢鏈塕Set鍒嗗尯(an RSet鈥檚 owning region)銆?/p>

Per Region Table

Per Region Table (PRT)

RSet鍦ㄥ唴閮ㄤ娇鐢≒er Region Table(PRT)璁板綍鍒嗗尯鐨勫紩鐢ㄦ儏鍐点€傜敱浜嶳Set鐨勮褰曡鍗犵敤鍒嗗尯鐨勭┖闂达紝濡傛灉涓€涓垎鍖洪潪甯?鍙楁杩?锛岄偅涔圧Set鍗犵敤鐨勭┖闂翠細涓婂崌锛屼粠鑰岄檷浣庡垎鍖虹殑鍙敤绌洪棿銆侴1搴斿杩欎釜闂閲囩敤浜嗘敼鍙楻Set鐨勫瘑搴︾殑鏂瑰紡锛屽湪PRT涓皢浼氫互涓夌妯″紡璁板綍寮曠敤锛?/p>

  • 绋€灏戯細鐩存帴璁板綍寮曠敤瀵硅薄鐨勫崱鐗囩储寮?/p>

  • 缁嗙矑搴︼細璁板綍寮曠敤瀵硅薄鐨勫垎鍖虹储寮?/p>

  • 绮楃矑搴︼細鍙褰曞紩鐢ㄦ儏鍐碉紝姣忎釜鍒嗗尯瀵瑰簲涓€涓瘮鐗逛綅

鐢变笂鍙煡锛岀矖绮掑害鐨凱RT鍙槸璁板綍浜嗗紩鐢ㄦ暟閲忥紝闇€瑕侀€氳繃鏁村爢鎵弿鎵嶈兘鎵惧嚭鎵€鏈夊紩鐢紝鍥犳鎵弿閫熷害涔熸槸鏈€鎱㈢殑銆?/p>

鏀堕泦闆嗗悎 (CSet)

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=鏀堕泦闆嗗悎 CSet

鏀堕泦闆嗗悎(CSet)浠h〃姣忔GC鏆傚仠鏃跺洖鏀剁殑涓€绯诲垪鐩爣鍒嗗尯銆傚湪浠绘剰涓€娆℃敹闆嗘殏鍋滀腑锛孋Set鎵€鏈夊垎鍖洪兘浼氳閲婃斁锛屽唴閮ㄥ瓨娲荤殑瀵硅薄閮戒細琚浆绉诲埌鍒嗛厤鐨勭┖闂插垎鍖轰腑銆傚洜姝ゆ棤璁烘槸骞磋交浠f敹闆嗭紝杩樻槸娣峰悎鏀堕泦锛屽伐浣滅殑鏈哄埗閮芥槸涓€鑷寸殑銆傚勾杞讳唬鏀堕泦CSet鍙绾冲勾杞讳唬鍒嗗尯锛岃€屾贩鍚堟敹闆嗕細閫氳繃鍚彂寮忕畻娉曪紝鍦ㄨ€佸勾浠e€欓€夊洖鏀跺垎鍖轰腑锛岀瓫閫夊嚭鍥炴敹鏀剁泭鏈€楂樼殑鍒嗗尯娣诲姞鍒癈Set涓€?/p>

鍊欓€夎€佸勾浠e垎鍖虹殑CSet鍑嗗叆鏉′欢锛屽彲浠ラ€氳繃娲昏穬搴﹂槇鍊?code class="mq-133">-XX:G1MixedGCLiveThresholdPercent(榛樿85%)杩涜璁剧疆锛屼粠鑰屾嫤鎴偅浜涘洖鏀跺紑閿€宸ㄥぇ鐨勫璞★紱鍚屾椂锛屾瘡娆℃贩鍚堟敹闆嗗彲浠ュ寘鍚€欓€夎€佸勾浠e垎鍖猴紝鍙牴鎹瓹Set瀵瑰爢鐨勬€诲ぇ灏忓崰姣?code class="mq-134">-XX:G1OldCSetRegionThresholdPercent(榛樿10%)璁剧疆鏁伴噺涓婇檺銆?/p>

鐢变笂杩板彲鐭ワ紝G1鐨勬敹闆嗛兘鏄牴鎹瓹Set杩涜鎿嶄綔鐨勶紝骞磋交浠f敹闆嗕笌娣峰悎鏀堕泦娌℃湁鏄庢樉鐨勪笉鍚岋紝鏈€澶х殑鍖哄埆鍦ㄤ簬涓ょ鏀堕泦鐨勮Е鍙戞潯浠躲€?/p>

骞磋交浠f敹闆嗛泦鍚?/h3>

骞磋交浠f敹闆嗛泦鍚?CSet of Young Collection

搴旂敤绾跨▼涓嶆柇娲诲姩鍚庯紝骞磋交浠g┖闂翠細琚€愭笎濉弧銆傚綋JVM鍒嗛厤瀵硅薄鍒癊den鍖哄煙澶辫触(Eden鍖哄凡婊?鏃讹紝渚夸細瑙﹀彂涓€娆TW寮忕殑骞磋交浠f敹闆嗐€傚湪骞磋交浠f敹闆嗕腑锛孍den鍒嗗尯瀛樻椿鐨勫璞″皢琚嫹璐濆埌Survivor鍒嗗尯锛涘師鏈塖urvivor鍒嗗尯瀛樻椿鐨勫璞★紝灏嗘牴鎹换鏈熼槇鍊?tenuring threshold)鍒嗗埆鏅嬪崌鍒癙LAB涓紝鏂扮殑survivor鍒嗗尯鍜岃€佸勾浠e垎鍖恒€傝€屽師鏈夌殑骞磋交浠e垎鍖哄皢琚暣浣撳洖鏀舵帀銆?/p>

鍚屾椂锛屽勾杞讳唬鏀堕泦杩樿礋璐g淮鎶ゅ璞$殑骞撮緞(瀛樻椿娆℃暟)锛岃緟鍔╁垽鏂€佸寲(tenuring)瀵硅薄鏅嬪崌鐨勬椂鍊欐槸鍒癝urvivor鍒嗗尯杩樻槸鍒拌€佸勾浠e垎鍖恒€傚勾杞讳唬鏀堕泦棣栧厛鍏堝皢鏅嬪崌瀵硅薄灏哄鎬诲拰銆佸璞″勾榫勪俊鎭淮鎶ゅ埌骞撮緞琛ㄤ腑锛屽啀鏍规嵁骞撮緞琛ㄣ€丼urvivor灏哄銆丼urvivor濉厖瀹归噺-XX:TargetSurvivorRatio(榛樿50%)銆佹渶澶т换鏈熼槇鍊?code class="mq-141">-XX:MaxTenuringThreshold(榛樿15)锛岃绠楀嚭涓€涓伆褰撶殑浠绘湡闃堝€硷紝鍑℃槸瓒呰繃浠绘湡闃堝€肩殑瀵硅薄閮戒細琚檵鍗囧埌鑰佸勾浠c€?/p>

娣峰悎鏀堕泦闆嗗悎

娣峰悎鏀堕泦闆嗗悎 CSet of Mixed Collection

骞磋交浠f敹闆嗕笉鏂椿鍔ㄥ悗锛岃€佸勾浠g殑绌洪棿涔熶細琚€愭笎濉厖銆傚綋鑰佸勾浠e崰鐢ㄧ┖闂磋秴杩囨暣鍫嗘瘮IHOP闃堝€?code class="mq-145">-XX:InitiatingHeapOccupancyPercent(榛樿45%)鏃讹紝G1灏变細鍚姩涓€娆℃贩鍚堝瀮鍦炬敹闆嗗懆鏈熴€備负浜嗘弧瓒虫殏鍋滅洰鏍囷紝G1鍙兘涓嶈兘涓€鍙f皵灏嗘墍鏈夌殑鍊欓€夊垎鍖烘敹闆嗘帀锛屽洜姝1鍙兘浼氫骇鐢熻繛缁娆$殑娣峰悎鏀堕泦涓庡簲鐢ㄧ嚎绋嬩氦鏇挎墽琛岋紝姣忔STW鐨勬贩鍚堟敹闆嗕笌骞磋交浠f敹闆嗚繃绋嬬浉绫讳技銆?/p>

涓轰簡纭畾鍖呭惈鍒板勾杞讳唬鏀堕泦闆嗗悎CSet鐨勮€佸勾浠e垎鍖猴紝JVM閫氳繃鍙傛暟娣峰悎鍛ㄦ湡鐨勬渶澶ф€绘鏁?code class="mq-147">-XX:G1MixedGCCountTarget(榛樿8)銆佸爢搴熺墿鐧惧垎姣?code class="mq-148">-XX:G1HeapWastePercent(榛樿5%)銆傞€氳繃鍊欓€夎€佸勾浠e垎鍖烘€绘暟涓庢贩鍚堝懆鏈熸渶澶ф€绘鏁帮紝纭畾姣忔鍖呭惈鍒癈Set鐨勬渶灏忓垎鍖烘暟閲忥紱鏍规嵁鍫嗗簾鐗╃櫨鍒嗘瘮锛屽綋鏀堕泦杈惧埌鍙傛暟鏃讹紝涓嶅啀鍚姩鏂扮殑娣峰悎鏀堕泦銆傝€屾瘡娆℃坊鍔犲埌CSet鐨勫垎鍖猴紝鍒欓€氳繃璁$畻寰楀埌鐨凣C鏁堢巼杩涜瀹夋帓銆?/p>

绗洓绔?G1鐨勬椿鍔ㄥ懆鏈?/h1>

G1鍨冨溇鏀堕泦娲诲姩姹囨€?/h2>

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=绁嚭涓€寮犳€诲浘

RSet鐨勭淮鎶?/h2>

鐢变簬涓嶈兘鏁村爢鎵弿锛屽張闇€瑕佽绠楀垎鍖虹‘鍒囩殑娲昏穬搴︼紝鍥犳锛孏1闇€瑕佷竴涓閲忓紡鐨勫畬鍏ㄦ爣璁板苟鍙戠畻娉曪紝閫氳繃缁存姢RSet锛屽緱鍒板噯纭殑鍒嗗尯寮曠敤淇℃伅銆傚湪G1涓紝RSet鐨勭淮鎶や富瑕佹潵婧愪袱涓柟闈細鍐欐爡鏍?Write Barrier)鍜屽苟鍙戜紭鍖栫嚎绋?Concurrence Refinement Threads)

鏍呮爮

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=鏍呮爮 Barrier

鎴戜滑棣栧厛浠嬬粛涓€涓嬫爡鏍?Barrier)鐨勬蹇点€傛爡鏍忔槸鎸囧湪鍘熺敓浠g爜鐗囨涓紝褰撴煇浜涜鍙ヨ鎵ц鏃讹紝鏍呮爮浠g爜涔熶細琚墽琛屻€傝€孏1涓昏鍦ㄨ祴鍊艰鍙ヤ腑锛屼娇鐢ㄥ啓鍓嶆爡鏍?Pre-Write Barrrier)鍜屽啓鍚庢爡鏍?Post-Write Barrrier)銆備簨瀹炰笂锛屽啓鏍呮爮鐨勬寚浠ゅ簭鍒楀紑閿€闈炲父鏄傝吹锛屽簲鐢ㄥ悶鍚愰噺涔熶細鏍规嵁鏍呮爮澶嶆潅搴﹁€岄檷浣庛€?/p>

鍐欏墠鏍呮爮 Pre-Write Barrrier

鍗冲皢鎵ц涓€娈佃祴鍊艰鍙ユ椂锛岀瓑寮忓乏渚у璞″皢淇敼寮曠敤鍒板彟涓€涓璞★紝閭d箞绛夊紡宸︿晶瀵硅薄鍘熷厛寮曠敤鐨勫璞℃墍鍦ㄥ垎鍖哄皢鍥犳涓уけ涓€涓紩鐢紝閭d箞JVM灏遍渶瑕佸湪璧嬪€艰鍙ョ敓鏁堜箣鍓嶏紝璁板綍涓уけ寮曠敤鐨勫璞°€侸VM骞朵笉浼氱珛鍗崇淮鎶Set锛岃€屾槸閫氳繃鎵归噺澶勭悊锛屽湪灏嗘潵RSet鏇存柊(瑙丼ATB)銆?/p>

鍐欏悗鏍呮爮 Post-Write Barrrier

褰撴墽琛屼竴娈佃祴鍊艰鍙ュ悗锛岀瓑寮忓彸渚у璞¤幏鍙栦簡宸︿晶瀵硅薄鐨勫紩鐢紝閭d箞绛夊紡鍙充晶瀵硅薄鎵€鍦ㄥ垎鍖虹殑RSet涔熷簲璇ュ緱鍒版洿鏂般€傚悓鏍蜂负浜嗛檷浣庡紑閿€锛屽啓鍚庢爡鏍忓彂鐢熷悗锛孯Set涔熶笉浼氱珛鍗虫洿鏂帮紝鍚屾牱鍙槸璁板綍姝ゆ鏇存柊鏃ュ織锛屽湪灏嗘潵鎵归噺澶勭悊(瑙丆oncurrence Refinement Threads)銆?/p>

璧峰蹇収绠楁硶

璧峰蹇収绠楁硶 Snapshot at the beginning (SATB)

Taiichi Tuasa璐$尞鐨勫閲忓紡瀹屽叏骞跺彂鏍囪绠楁硶璧峰蹇収绠楁硶(SATB)锛屼富瑕侀拡瀵规爣璁?娓呴櫎鍨冨溇鏀堕泦鍣ㄧ殑骞跺彂鏍囪闃舵锛岄潪甯搁€傚悎G1鐨勫垎鍖哄潡鐨勫爢缁撴瀯锛屽悓鏃惰В鍐充簡CMS鐨勪富瑕佺儲鎭硷細閲嶆柊鏍囪鏆傚仠鏃堕棿闀垮甫鏉ョ殑娼滃湪椋庨櫓銆?/p>

SATB浼氬垱寤轰竴涓璞″浘锛岀浉褰撲簬鍫嗙殑閫昏緫蹇収锛屼粠鑰岀‘淇濆苟鍙戞爣璁伴樁娈垫墍鏈夌殑鍨冨溇瀵硅薄閮借兘閫氳繃蹇収琚壌鍒嚭鏉ャ€傚綋璧嬪€艰鍙ュ彂鐢熸椂锛屽簲鐢ㄥ皢浼氭敼鍙樹簡瀹冪殑瀵硅薄鍥撅紝閭d箞JVM闇€瑕佽褰曡瑕嗙洊鐨勫璞°€傚洜姝ゅ啓鍓嶆爡鏍忎細鍦ㄥ紩鐢ㄥ彉鏇村墠锛屽皢鍊艰褰曞湪SATB鏃ュ織鎴栫紦鍐插尯涓€傛瘡涓嚎绋嬮兘浼氱嫭鍗犱竴涓猄ATB缂撳啿鍖猴紝鍒濆鏈?56鏉¤褰曠┖闂淬€傚綋绌洪棿鐢ㄥ敖鏃讹紝绾跨▼浼氬垎閰嶆柊鐨凷ATB缂撳啿鍖虹户缁娇鐢紝鑰屽師鏈夌殑缂撳啿鍘诲垯鍔犲叆鍏ㄥ眬鍒楄〃涓€傛渶缁堝湪骞跺彂鏍囪闃舵锛屽苟鍙戞爣璁扮嚎绋?Concurrent Marking Threads)鍦ㄦ爣璁扮殑鍚屾椂锛岃繕浼氬畾鏈熸鏌ュ拰澶勭悊鍏ㄥ眬缂撳啿鍖哄垪琛ㄧ殑璁板綍锛岀劧鍚庢牴鎹爣璁颁綅鍥惧垎鐗囩殑鏍囪浣嶏紝鎵弿寮曠敤瀛楁鏉ユ洿鏂癛Set銆傛杩囩▼鍙堢О涓哄苟鍙戞爣璁?SATB鍐欏墠鏍呮爮銆?/p>

骞跺彂浼樺寲绾跨▼

骞跺彂浼樺寲绾跨▼ Concurrence Refinement Threads

G1涓娇鐢ㄥ熀浜嶶rs H枚lzle鐨勫揩閫熷啓鏍呮爮锛屽皢鏍呮爮寮€閿€缂╁噺鍒?涓澶栫殑鎸囦护銆傛爡鏍忓皢浼氭洿鏂颁竴涓猚ard table type鐨勭粨鏋勬潵璺熻釜浠i棿寮曠敤銆?/p>

褰撹祴鍊艰鍙ュ彂鐢熷悗锛屽啓鍚庢爡鏍忎細鍏堥€氳繃G1鐨勮繃婊ゆ妧鏈垽鏂槸鍚︽槸璺ㄥ垎鍖虹殑寮曠敤鏇存柊锛屽苟灏嗚法鍒嗗尯鏇存柊瀵硅薄鐨勫崱鐗囧姞鍏ョ紦鍐插尯搴忓垪锛屽嵆鏇存柊鏃ュ織缂撳啿鍖烘垨鑴忓崱鐗囬槦鍒椼€備笌SATB绫讳技锛屼竴鏃︽棩蹇楃紦鍐插尯鐢ㄥ敖锛屽垯鍒嗛厤涓€涓柊鐨勬棩蹇楃紦鍐插尯锛屽苟灏嗗師鏉ョ殑缂撳啿鍖哄姞鍏ュ叏灞€鍒楄〃涓€?/p>

骞跺彂浼樺寲绾跨▼(Concurrence Refinement Threads)锛屽彧涓撴敞鎵弿鏃ュ織缂撳啿鍖鸿褰曠殑鍗$墖鏉ョ淮鎶ゆ洿鏂癛Set锛岀嚎绋嬫渶澶ф暟鐩彲閫氳繃-XX:G1ConcRefinementThreads(榛樿绛変簬-XX:ParellelGCThreads)璁剧疆銆傚苟鍙戜紭鍖栫嚎绋嬫案杩滄槸娲昏穬鐨勶紝涓€鏃﹀彂鐜板叏灞€鍒楄〃鏈夎褰曞瓨鍦紝灏卞紑濮嬪苟鍙戝鐞嗐€傚鏋滆褰曞闀垮緢蹇垨鑰呮潵涓嶅強澶勭悊锛岄偅涔堥€氳繃闃堝€?code class="mq-176">-X:G1ConcRefinementGreenZone/-XX:G1ConcRefinementYellowZone/-XX:G1ConcRefinementRedZone锛孏1浼氱敤鍒嗗眰鐨勬柟寮忚皟搴︼紝浣挎洿澶氱殑绾跨▼澶勭悊鍏ㄥ眬鍒楄〃銆傚鏋滃苟鍙戜紭鍖栫嚎绋嬩篃涓嶈兘璺熶笂缂撳啿鍖烘暟閲忥紝鍒橫utator绾跨▼(Java搴旂敤绾跨▼)浼氭寕璧峰簲鐢ㄥ苟琚姞杩涙潵甯姪澶勭悊锛岀洿鍒板叏閮ㄥ鐞嗗畬銆傚洜姝わ紝蹇呴』閬垮厤姝ょ被鍦烘櫙鍑虹幇銆?/p>

骞跺彂鏍囪鍛ㄦ湡

骞跺彂鏍囪鍛ㄦ湡 Concurrent Marking Cycle

骞跺彂鏍囪鍛ㄦ湡鏄疓1涓潪甯搁噸瑕佺殑闃舵锛岃繖涓樁娈靛皢浼氫负娣峰悎鏀堕泦鍛ㄦ湡璇嗗埆鍨冨溇鏈€澶氱殑鑰佸勾浠e垎鍖恒€傛暣涓懆鏈熷畬鎴愭牴鏍囪銆佽瘑鍒墍鏈?鍙兘)瀛樻椿瀵硅薄锛屽苟璁$畻姣忎釜鍒嗗尯鐨勬椿璺冨害锛屼粠鑰岀‘瀹欸C鏁堢巼绛夌骇銆?/p>

褰撹揪鍒癐HOP闃堝€?code class="mq-183">-XX:InitiatingHeapOccupancyPercent(鑰佸勾浠e崰鏁村爢姣旓紝榛樿45%)鏃讹紝渚夸細瑙﹀彂骞跺彂鏍囪鍛ㄦ湡銆傛暣涓苟鍙戞爣璁板懆鏈熷皢鐢卞垵濮嬫爣璁?Initial Mark)銆佹牴鍒嗗尯鎵弿(Root Region Scanning)銆佸苟鍙戞爣璁?Concurrent Marking)銆侀噸鏂版爣璁?Remark)銆佹竻闄?Cleanup)鍑犱釜闃舵缁勬垚銆傚叾涓紝鍒濆鏍囪(闅忓勾杞讳唬鏀堕泦涓€璧锋椿鍔?銆侀噸鏂版爣璁般€佹竻闄ゆ槸STW鐨勶紝鑰屽苟鍙戞爣璁板鏋滄潵涓嶅強鏍囪瀛樻椿瀵硅薄锛屽垯鍙兘鍦ㄥ苟鍙戞爣璁拌繃绋嬩腑锛孏1鍙堣Е鍙戜簡鍑犳骞磋交浠f敹闆嗐€?/p>

骞跺彂鏍囪绾跨▼

骞跺彂鏍囪绾跨▼ Concurrent Marking Threads

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?></p> 
<p class=瑕佹爣璁板瓨娲荤殑瀵硅薄锛屾瘡涓垎鍖洪兘闇€瑕佸垱寤轰綅鍥?Bitmap)淇℃伅鏉ュ瓨鍌ㄦ爣璁版暟鎹紝鏉ョ‘瀹氭爣璁板懆鏈熷唴琚垎閰嶇殑瀵硅薄銆侴1閲囩敤浜嗕袱涓綅鍥綪revious Bitmap銆丯ext Bitmap锛屾潵瀛樺偍鏍囪鏁版嵁锛孭revious浣嶅浘瀛樺偍涓婃鐨勬爣璁版暟鎹紝Next浣嶅浘鍦ㄦ爣璁板懆鏈熷唴涓嶆柇鍙樺寲鏇存柊锛屽悓鏃禤revious浣嶅浘鐨勬爣璁版暟鎹篃瓒婃潵瓒婅繃鏃讹紝褰撴爣璁板懆鏈熺粨鏉熷悗Next浣嶅浘渚挎浛鎹revious浣嶅浘锛屾垚涓轰笂娆℃爣璁扮殑浣嶅浘銆傚悓鏃讹紝姣忎釜鍒嗗尯閫氳繃椤堕儴寮€濮嬫爣璁?TAMS)锛屾潵璁板綍宸叉爣璁拌繃鐨勫唴瀛樿寖鍥淬€傚悓鏍风殑锛孏1浣跨敤浜嗕袱涓《閮ㄥ紑濮嬫爣璁癙revious TAMS(PTAMS)銆丯ext TAMS(NTAMS)锛岃褰曞凡鏍囪鐨勮寖鍥淬€?br>

鍦ㄥ苟鍙戞爣璁伴樁娈碉紝G1浼氭牴鎹弬鏁?code class="mq-190">-XX:ConcGCThreads(榛樿GC绾跨▼鏁扮殑1/4锛屽嵆-XX:ParallelGCThreads/4)锛屽垎閰嶅苟鍙戞爣璁扮嚎绋?Concurrent Marking Threads)锛岃繘琛屾爣璁版椿鍔ㄣ€傛瘡涓苟鍙戠嚎绋嬩竴娆″彧鎵弿涓€涓垎鍖猴紝骞堕€氳繃"鎵嬫寚"鎸囬拡鐨勬柟寮忎紭鍖栬幏鍙栧垎鍖恒€傚苟鍙戞爣璁扮嚎绋嬫槸鐖嗗彂寮忕殑锛屽湪缁欏畾鐨勬椂闂存鎷煎懡骞叉椿锛岀劧鍚庝紤鎭竴娈垫椂闂达紝鍐嶆嫾鍛藉共娲汇€?/p>

鍒濆鏍囪

鍒濆鏍囪 Initial Mark

鍒濆鏍囪(Initial Mark)璐熻矗鏍囪鎵€鏈夎兘琚洿鎺ュ彲杈剧殑鏍瑰璞?鍘熺敓鏍堝璞°€佸叏灞€瀵硅薄銆丣NI瀵硅薄)锛屾牴鏄璞″浘鐨勮捣鐐癸紝鍥犳鍒濆鏍囪闇€瑕佸皢Mutator绾跨▼(Java搴旂敤绾跨▼)鏆傚仠鎺夛紝涔熷氨鏄渶瑕佷竴涓猄TW鐨勬椂闂存銆備簨瀹炰笂锛屽綋杈惧埌IHOP闃堝€兼椂锛孏1骞朵笉浼氱珛鍗冲彂璧峰苟鍙戞爣璁板懆鏈燂紝鑰屾槸绛夊緟涓嬩竴娆″勾杞讳唬鏀堕泦锛屽埄鐢ㄥ勾杞讳唬鏀堕泦鐨凷TW鏃堕棿娈碉紝瀹屾垚鍒濆鏍囪锛岃繖绉嶆柟寮忕О涓哄€熼亾(Piggybacking)銆傚湪鍒濆鏍囪鏆傚仠涓紝鍒嗗尯鐨凬TAMS閮借璁剧疆鍒板垎鍖洪《閮═op锛屽垵濮嬫爣璁版槸骞跺彂鎵ц锛岀洿鍒版墍鏈夌殑鍒嗗尯澶勭悊瀹屻€?/p>

鏍瑰垎鍖烘壂鎻?/h3>

鏍瑰垎鍖烘壂鎻?Root Region Scanning

鍦ㄥ垵濮嬫爣璁版殏鍋滅粨鏉熷悗锛屽勾杞讳唬鏀堕泦涔熷畬鎴愮殑瀵硅薄澶嶅埗鍒癝urvivor鐨勫伐浣滐紝搴旂敤绾跨▼寮€濮嬫椿璺冭捣鏉ャ€傛鏃朵负浜嗕繚璇佹爣璁扮畻娉曠殑姝g‘鎬э紝鎵€鏈夋柊澶嶅埗鍒癝urvivor鍒嗗尯鐨勫璞★紝閮介渶瑕佽鎵弿骞舵爣璁版垚鏍癸紝杩欎釜杩囩▼绉颁负鏍瑰垎鍖烘壂鎻?Root Region Scanning)锛屽悓鏃舵壂鎻忕殑Suvivor鍒嗗尯涔熻绉颁负鏍瑰垎鍖?Root Region)銆傛牴鍒嗗尯鎵弿蹇呴』鍦ㄤ笅涓€娆″勾杞讳唬鍨冨溇鏀堕泦鍚姩鍓嶅畬鎴?骞跺彂鏍囪鐨勮繃绋嬩腑锛屽彲鑳戒細琚嫢骞叉骞磋交浠e瀮鍦炬敹闆嗘墦鏂?锛屽洜涓烘瘡娆C浼氫骇鐢熸柊鐨勫瓨娲诲璞¢泦鍚堛€?/p>

骞跺彂鏍囪

骞跺彂鏍囪 Concurrent Marking

鍜屽簲鐢ㄧ嚎绋嬪苟鍙戞墽琛岋紝骞跺彂鏍囪绾跨▼鍦ㄥ苟鍙戞爣璁伴樁娈靛惎鍔紝鐢卞弬鏁?code class="mq-201">-XX:ConcGCThreads(榛樿GC绾跨▼鏁扮殑1/4锛屽嵆-XX:ParallelGCThreads/4)鎺у埗鍚姩鏁伴噺锛屾瘡涓嚎绋嬫瘡娆″彧鎵弿涓€涓垎鍖猴紝浠庤€屾爣璁板嚭瀛樻椿瀵硅薄鍥俱€傚湪杩欎竴闃舵浼氬鐞哖revious/Next鏍囪浣嶅浘锛屾壂鎻忔爣璁板璞$殑寮曠敤瀛楁銆傚悓鏃讹紝骞跺彂鏍囪绾跨▼杩樹細瀹氭湡妫€鏌ュ拰澶勭悊STAB鍏ㄥ眬缂撳啿鍖哄垪琛ㄧ殑璁板綍锛屾洿鏂板璞″紩鐢ㄤ俊鎭€傚弬鏁?code class="mq-203">-XX:+ClassUnloadingWithConcurrentMark浼氬紑鍚竴涓紭鍖栵紝濡傛灉涓€涓被涓嶅彲杈?涓嶆槸瀵硅薄涓嶅彲杈?锛屽垯鍦ㄩ噸鏂版爣璁伴樁娈碉紝杩欎釜绫诲氨浼氳鐩存帴鍗歌浇銆傛墍鏈夌殑鏍囪浠诲姟蹇呴』鍦ㄥ爢婊″墠灏卞畬鎴愭壂鎻忥紝濡傛灉骞跺彂鏍囪鑰楁椂寰堥暱锛岄偅涔堟湁鍙兘鍦ㄥ苟鍙戞爣璁拌繃绋嬩腑锛屽張缁忓巻浜嗗嚑娆″勾杞讳唬鏀堕泦銆傚鏋滃爢婊″墠娌℃湁瀹屾垚鏍囪浠诲姟锛屽垯浼氳Е鍙戞媴淇濇満鍒讹紝缁忓巻涓€娆¢暱鏃堕棿鐨勪覆琛孎ull GC銆?/p>

瀛樻椿鏁版嵁璁$畻

瀛樻椿鏁版嵁璁$畻 Live Data Accounting

瀛樻椿鏁版嵁璁$畻(Live Data Accounting)鏄爣璁版搷浣滅殑闄勫姞浜х墿锛屽彧瑕佷竴涓璞¤鏍囪锛屽悓鏃朵細琚绠楀瓧鑺傛暟锛屽苟璁″叆鍒嗗尯绌洪棿銆傚彧鏈塏TAMS浠ヤ笅鐨勫璞′細琚爣璁板拰璁$畻锛屽湪鏍囪鍛ㄦ湡鐨勬渶鍚庯紝Next浣嶅浘灏嗚娓呯┖锛岀瓑寰呬笅娆℃爣璁板懆鏈熴€?/p>

閲嶆柊鏍囪

閲嶆柊鏍囪 Remark

閲嶆柊鏍囪(Remark)鏄渶鍚庝竴涓爣璁伴樁娈点€傚湪璇ラ樁娈典腑锛孏1闇€瑕佷竴涓殏鍋滅殑鏃堕棿锛屽幓澶勭悊鍓╀笅鐨凷ATB鏃ュ織缂撳啿鍖哄拰鎵€鏈夋洿鏂帮紝鎵惧嚭鎵€鏈夋湭琚闂殑瀛樻椿瀵硅薄锛屽悓鏃跺畨鍏ㄥ畬鎴愬瓨娲绘暟鎹绠椼€傝繖涓樁娈典篃鏄苟琛屾墽琛岀殑锛岄€氳繃鍙傛暟-XX:ParallelGCThread鍙缃瓽C鏆傚仠鏃跺彲鐢ㄧ殑GC绾跨▼鏁般€傚悓鏃讹紝寮曠敤澶勭悊涔熸槸閲嶆柊鏍囪闃舵鐨勪竴閮ㄥ垎锛屾墍鏈夐噸搴︿娇鐢ㄥ紩鐢ㄥ璞?寮卞紩鐢ㄣ€佽蒋寮曠敤銆佽櫄寮曠敤銆佹渶缁堝紩鐢?鐨勫簲鐢ㄩ兘浼氬湪寮曠敤澶勭悊涓婁骇鐢熷紑閿€銆?/p>

娓呴櫎

娓呴櫎 Cleanup

绱ф尐鐫€閲嶆柊鏍囪闃舵鐨勬竻闄?Clean)闃舵涔熸槸STW鐨勩€侾revious/Next鏍囪浣嶅浘銆佷互鍙奝TAMS/NTAMS锛岄兘浼氬湪娓呴櫎闃舵浜ゆ崲瑙掕壊銆傛竻闄ら樁娈典富瑕佹墽琛屼互涓嬫搷浣滐細

  1. RSet姊崇悊锛屽惎鍙戝紡绠楁硶浼氭牴鎹椿璺冨害鍜孯Set灏哄瀵瑰垎鍖哄畾涔変笉鍚岀瓑绾э紝鍚屾椂RSet鏁扮悊涔熸湁鍔╀簬鍙戠幇鏃犵敤鐨勫紩鐢ㄣ€傚弬鏁?code class="mq-215">-XX:+PrintAdaptiveSizePolicy鍙互寮€鍚墦鍗板惎鍙戝紡绠楁硶鍐崇瓥缁嗚妭锛?/p>

  2. 鏁寸悊鍫嗗垎鍖猴紝涓烘贩鍚堟敹闆嗗懆鏈熻瘑鍒洖鏀舵敹鐩婇珮(鍩轰簬閲婃斁绌洪棿鍜屾殏鍋滅洰鏍?鐨勮€佸勾浠e垎鍖洪泦鍚堬紱

  3. 璇嗗埆鎵€鏈夌┖闂插垎鍖猴紝鍗冲彂鐜版棤瀛樻椿瀵硅薄鐨勫垎鍖恒€傝鍒嗗尯鍙湪娓呴櫎闃舵鐩存帴鍥炴敹锛屾棤闇€绛夊緟涓嬫鏀堕泦鍛ㄦ湡銆?/p>

骞磋交浠f敹闆?娣峰悎鏀堕泦鍛ㄦ湡

骞磋交浠f敹闆嗗拰娣峰悎鏀堕泦鍛ㄦ湡锛屾槸G1鍥炴敹绌洪棿鐨勪富瑕佹椿鍔ㄣ€傚綋搴旂敤杩愯寮€濮嬫椂锛屽爢鍐呭瓨鍙敤绌洪棿杩樻瘮杈冨ぇ锛屽彧浼氬湪骞磋交浠f弧鏃讹紝瑙﹀彂骞磋交浠f敹闆嗭紱闅忕潃鑰佸勾浠e唴瀛樺闀匡紝褰撳埌杈綢HOP闃堝€?code class="mq-218">-XX:InitiatingHeapOccupancyPercent(鑰佸勾浠e崰鏁村爢姣旓紝榛樿45%)鏃讹紝G1寮€濮嬬潃鎵嬪噯澶囨敹闆嗚€佸勾浠g┖闂淬€傞鍏堢粡鍘嗗苟鍙戞爣璁板懆鏈燂紝璇嗗埆鍑洪珮鏀剁泭鐨勮€佸勾浠e垎鍖猴紝鍓嶆枃宸茶堪銆備絾闅忓悗G1骞朵笉浼氶┈涓婂紑濮嬩竴娆℃贩鍚堟敹闆嗭紝鑰屾槸璁╁簲鐢ㄧ嚎绋嬪厛杩愯涓€娈垫椂闂达紝绛夊緟瑙﹀彂涓€娆″勾杞讳唬鏀堕泦銆傚湪杩欐STW涓紝G1灏嗕繚鍑嗘暣鐞嗘贩鍚堟敹闆嗗懆鏈熴€傛帴鐫€鍐嶆璁╁簲鐢ㄧ嚎绋嬭繍琛岋紝褰撴帴涓嬫潵鐨勫嚑娆″勾杞讳唬鏀堕泦鏃讹紝灏嗕細鏈夎€佸勾浠e垎鍖哄姞鍏ュ埌CSet涓紝鍗宠Е鍙戞贩鍚堟敹闆嗭紝杩欎簺杩炵画澶氭鐨勬贩鍚堟敹闆嗙О涓烘贩鍚堟敹闆嗗懆鏈?Mixed Collection Cycle)銆?/p>

GC宸ヤ綔绾跨▼鏁?/h3>

GC宸ヤ綔绾跨▼鏁?-XX:ParallelGCThreads

JVM鍙互閫氳繃鍙傛暟-XX:ParallelGCThreads杩涜鎸囧畾GC宸ヤ綔鐨勭嚎绋嬫暟閲忋€傚弬鏁?code class="mq-223">-XX:ParallelGCThreads榛樿鍊煎苟涓嶆槸鍥哄畾鐨勶紝鑰屾槸鏍规嵁褰撳墠鐨凜PU璧勬簮杩涜璁$畻銆傚鏋滅敤鎴锋病鏈夋寚瀹氾紝涓擟PU灏忎簬绛変簬8锛屽垯榛樿涓嶤PU鏍告暟鐩哥瓑锛涜嫢CPU澶т簬8锛屽垯榛樿JVM浼氱粡杩囪绠楀緱鍒颁竴涓皬浜嶤PU鏍告暟鐨勭嚎绋嬫暟锛涘綋鐒朵篃鍙互浜哄伐鎸囧畾涓嶤PU鏍告暟鐩哥瓑銆?/p>

骞磋交浠f敹闆?/h3>

骞磋交浠f敹闆?Young Collection

姣忔鏀堕泦杩囩▼涓紝鏃㈡湁骞惰鎵ц鐨勬椿鍔紝涔熸湁涓茶鎵ц鐨勬椿鍔紝浣嗛兘鍙互鏄绾跨▼鐨勩€傚湪骞惰鎵ц鐨勪换鍔′腑锛屽鏋滄煇涓换鍔¤繃閲嶏紝浼氬鑷村叾浠栫嚎绋嬪湪绛夊緟鏌愰」浠诲姟鐨勫鐞嗭紝闇€瑕佸杩欎簺鍦版柟杩涜浼樺寲銆?/p>

骞惰娲诲姩

澶栭儴鏍瑰垎鍖烘壂鎻?Ext Root Scanning锛氭娲诲姩瀵瑰爢澶栫殑鏍?JVM绯荤粺鐩綍銆乂M鏁版嵁缁撴瀯銆丣NI绾跨▼鍙ユ焺銆佺‖浠跺瘎瀛樺櫒銆佸叏灞€鍙橀噺銆佺嚎绋嬪鏍堟牴)杩涜鎵弿锛屽彂鐜伴偅浜涙病鏈夊姞鍏ュ埌鏆傚仠鏀堕泦闆嗗悎CSet涓殑瀵硅薄銆傚鏋滅郴缁熺洰褰?鍗曟牴)鎷ユ湁澶ч噺鍔犺浇鐨勭被锛屾渶缁堝彲鑳藉叾浠栧苟琛屾椿鍔ㄧ粨鏉熷悗锛岃娲诲姩渚濈劧娌℃湁缁撴潫鑰屽甫鏉ョ殑绛夊緟鏃堕棿銆?/p>

鏇存柊宸茶蹇嗛泦鍚?Update RS锛氬苟鍙戜紭鍖栫嚎绋嬩細瀵硅剰鍗$墖鐨勫垎鍖鸿繘琛屾壂鎻忔洿鏂版棩蹇楃紦鍐插尯鏉ユ洿鏂癛Set锛屼絾鍙細澶勭悊鍏ㄥ眬缂撳啿鍒楄〃銆備綔涓鸿ˉ鍏咃紝鎵€鏈夎璁板綍浣嗘槸杩樻病鏈夎浼樺寲绾跨▼澶勭悊鐨勫墿浣欑紦鍐插尯锛屼細鍦ㄨ闃舵澶勭悊锛屽彉鎴愬凡澶勭悊缂撳啿鍖?Processed Buffers)銆備负浜嗛檺鍒惰姳鍦ㄦ洿鏂癛Set鐨勬椂闂达紝鍙互璁剧疆鏆傚仠鍗犵敤鐧惧垎姣?code class="mq-230">-XX:G1RSetUpdatingPauseTimePercent(榛樿10%锛屽嵆-XX:MaxGCPauseMills/10)銆傚€煎緱娉ㄦ剰鐨勬槸锛屽鏋滄洿鏂版棩蹇楃紦鍐插尯鏇存柊鐨勪换鍔′笉闄嶄綆锛屽崟绾湴鍑忓皯RSet鐨勬洿鏂版椂闂达紝浼氬鑷存殏鍋滀腑琚鐞嗙殑缂撳啿鍖哄噺灏戯紝灏嗘棩蹇楃紦鍐插尯鏇存柊宸ヤ綔鎺ㄥ埌骞跺彂浼樺寲绾跨▼涓婏紝浠庤€屽鍔犲Java搴旂敤绾跨▼璧勬簮鐨勪簤澶恒€?/p>

RSet鎵弿 Scan RS锛氬湪鏀堕泦褰撳墠CSet涔嬪墠锛岃€冭檻鍒板垎鍖哄鐨勫紩鐢紝蹇呴』鎵弿CSet鍒嗗尯鐨凴Set銆傚鏋淩Set鍙戠敓绮楀寲锛屽垯浼氬鍔燫Set鐨勬壂鎻忔椂闂淬€傚紑鍚瘖鏂ā寮?code class="mq-233">-XX:UnlockDiagnosticVMOptions鍚庯紝閫氳繃鍙傛暟-XX:+G1SummarizeRSetStats鍙互纭畾骞跺彂浼樺寲绾跨▼鏄惁鑳藉鍙婃椂澶勭悊鏇存柊鏃ュ織缂撳啿鍖猴紝骞舵彁渚涙洿澶氱殑淇℃伅锛屾潵甯姪涓篟Set绮楀寲鎬绘暟鎻愪緵绐楀彛銆傚弬鏁?code class="mq-235">-XX锛欸1SummarizeRSetStatsPeriod=n鍙缃甊Set鐨勭粺璁″懆鏈燂紝鍗崇粡鍘嗗灏戞GC鍚庤繘琛屼竴娆$粺璁?/p>

浠g爜鏍规壂鎻?Code Root Scanning锛氬浠g爜鏍归泦鍚堣繘琛屾壂鎻忥紝鎵弿JVM缂栬瘧鍚庝唬鐮丯ative Method鐨勫紩鐢ㄤ俊鎭?nmethod鎵弿)锛岃繘琛孯Set鎵弿銆備簨瀹炰笂锛屽彧鏈塁Set鍒嗗尯涓殑RSet鏈夊己浠g爜鏍规椂锛屾墠浼氬仛nmethod鎵弿锛屾煡鎵惧CSet鐨勫紩鐢ㄣ€?/p>

杞Щ鍜屽洖鏀?Object Copy锛氶€氳繃閫夊畾鐨凜Set浠ュ強CSet鍒嗗尯瀹屾暣鐨勫紩鐢ㄩ泦锛屽皢鎵ц鏆傚仠鏃堕棿鐨勪富瑕侀儴鍒嗭細CSet鍒嗗尯瀛樻椿瀵硅薄鐨勮浆绉汇€丆Set鍒嗗尯绌洪棿鐨勫洖鏀躲€傞€氳繃宸ヤ綔绐冨彇鏈哄埗鏉ヨ礋杞藉潎琛″湴閫夊畾澶嶅埗瀵硅薄鐨勭嚎绋嬶紝骞朵笖澶嶅埗鍜屾壂鎻忓璞¤杞Щ鐨勫瓨娲诲璞″皢鎷疯礉鍒版瘡涓狦C绾跨▼鍒嗛厤缂撳啿鍖篏CLAB銆侴1浼氶€氳繃璁$畻锛岄娴嬪垎鍖哄鍒舵墍鑺辫垂鐨勬椂闂达紝浠庤€岃皟鏁村勾杞讳唬鐨勫昂瀵搞€?/p>

缁堟 Termination锛氬畬鎴愪笂杩颁换鍔″悗锛屽鏋滀换鍔¢槦鍒楀凡绌猴紝鍒欏伐浣滅嚎绋嬩細鍙戣捣缁堟瑕佹眰銆傚鏋滆繕鏈夊叾浠栫嚎绋嬬户缁伐浣滐紝绌洪棽鐨勭嚎绋嬩細閫氳繃宸ヤ綔绐冨彇鏈哄埗灏濊瘯甯姪鍏朵粬绾跨▼澶勭悊銆傝€屽崟鐙墽琛屾牴鍒嗗尯鎵弿鐨勭嚎绋嬶紝濡傛灉浠诲姟杩囬噸锛屾渶缁堜細鏅氫簬缁堟銆?/p>

GC澶栭儴鐨勫苟琛屾椿鍔?GC Worker Other锛氳閮ㄥ垎骞堕潪GC鐨勬椿鍔紝鑰屾槸JVM鐨勬椿鍔ㄥ鑷村崰鐢ㄤ簡GC鏆傚仠鏃堕棿(渚嬪JNI缂栬瘧)銆?/p>

涓茶娲诲姩

浠g爜鏍规洿鏂?Code Root Fixup锛氭牴鎹浆绉诲璞℃洿鏂颁唬鐮佹牴銆?/p>

浠g爜鏍规竻鐞?Code Root Purge锛氭竻鐞嗕唬鐮佹牴闆嗗悎琛ㄣ€?/p>

娓呴櫎鍏ㄥ眬鍗$墖鏍囪 Clear CT锛氬湪浠绘剰鏀堕泦鍛ㄦ湡浼氭壂鎻廋Set涓嶳Set璁板綍鐨凱RT锛屾壂鎻忔椂浼氬湪鍏ㄥ眬鍗$墖琛ㄤ腑杩涜鏍囪锛岄槻姝㈤噸澶嶆壂鎻忋€傚湪鏀堕泦鍛ㄦ湡鐨勬渶鍚庡皢浼氭竻闄ゅ叏灞€鍗$墖琛ㄤ腑鐨勫凡鎵弿鏍囧織銆?/p>

閫夋嫨涓嬫鏀堕泦闆嗗悎 Choose CSet锛氳閮ㄥ垎涓昏鐢ㄤ簬骞跺彂鏍囪鍛ㄦ湡鍚庣殑骞磋交浠f敹闆嗐€佷互鍙婃贩鍚堟敹闆嗕腑锛屽湪杩欎簺鏀堕泦杩囩▼涓紝鐢变簬鏈夎€佸勾浠e€欓€夊垎鍖虹殑鍔犲叆锛屽線寰€闇€瑕佸涓嬫鏀堕泦鐨勮寖鍥村仛鍑虹晫瀹氾紱浣嗗崟绾殑骞磋交浠f敹闆嗕腑锛屾墍鏈夋敹闆嗙殑鍒嗗尯閮戒細琚敹闆嗭紝涓嶅瓨鍦ㄩ€夋嫨銆?/p>

寮曠敤澶勭悊 Ref Proc锛氫富瑕侀拡瀵硅蒋寮曠敤銆佸急寮曠敤銆佽櫄寮曠敤銆乫inal寮曠敤銆丣NI寮曠敤銆傚綋Ref Proc鍗犵敤鏃堕棿杩囧鏃讹紝鍙€夋嫨浣跨敤鍙傛暟-XX:ParallelRefProcEnabled婵€娲诲绾跨▼寮曠敤澶勭悊銆侴1甯屾湜搴旂敤鑳藉皬蹇冧娇鐢ㄨ蒋寮曠敤锛屽洜涓鸿蒋寮曠敤浼氫竴鐩村崰鎹唴瀛樼┖闂寸洿鍒扮┖闂磋€楀敖鏃惰Full GC鍥炴敹鎺夛紱鍗充娇鏈彂鐢烣ull GC锛岃蒋寮曠敤瀵瑰唴瀛樼殑鍗犵敤锛屼篃浼氬鑷碐C娆℃暟鐨勫鍔犮€?/p>

寮曠敤鎺掗槦 Ref Enq锛氭椤规椿鍔ㄥ彲鑳戒細瀵艰嚧RSet鐨勬洿鏂帮紝姝ゆ椂浼氶€氳繃璁板綍鏃ュ織锛屽皢鍏宠仈鐨勫崱鐗囨爣璁颁负鑴忓崱鐗囥€?/p>

鍗$墖閲嶆柊鑴忓寲 Redirty Cards锛氶噸鏂拌剰鍖栧崱鐗囥€?/p>

鍥炴敹绌洪棽宸ㄥ瀷鍒嗗尯 Humongous Reclaim锛欸1鍋氫簡涓€涓紭鍖栵細閫氳繃鏌ョ湅鎵€鏈夋牴瀵硅薄浠ュ強骞磋交浠e垎鍖虹殑RSet锛屽鏋滅‘瀹歊Set涓法鍨嬪璞℃病鏈変换浣曞紩鐢紝鍒欒鏄嶨1鍙戠幇浜嗕竴涓笉鍙揪鐨勫法鍨嬪璞★紝璇ュ璞″垎鍖轰細琚洖鏀躲€?/p>

閲婃斁鍒嗗尯 Free CSet锛氬洖鏀禖Set鍒嗗尯鐨勬墍鏈夌┖闂达紝骞跺姞鍏ュ埌绌洪棽鍒嗗尯涓€?/p>

鍏朵粬娲诲姩 Other锛欸C涓彲鑳借繕浼氱粡鍘嗗叾浠栬€楁椂寰堝皬鐨勬椿鍔紝濡備慨澶岼NI鍙ユ焺绛夈€?/p>

骞跺彂鏍囪鍛ㄦ湡鍚庣殑骞磋交浠f敹闆?/h3>

骞跺彂鏍囪鍛ㄦ湡鍚庣殑骞磋交浠f敹闆?Young Collection Following Concurrent Marking Cycle

褰揋1鍙戣捣骞跺彂鏍囪鍛ㄦ湡涔嬪悗锛屽苟涓嶄細椹笂寮€濮嬫贩鍚堟敹闆嗐€侴1浼氬厛绛夊緟涓嬩竴娆″勾杞讳唬鏀堕泦锛岀劧鍚庡湪璇ユ敹闆嗛樁娈典腑锛岀‘瀹氫笅娆℃贩鍚堟敹闆嗙殑CSet(Choose CSet)銆?/p>

娣峰悎鏀堕泦鍛ㄦ湡

娣峰悎鏀堕泦鍛ㄦ湡 Mixed Collection Cycle

鍗曟鐨勬贩鍚堟敹闆嗕笌骞磋交浠f敹闆嗗苟鏃犱簩鑷淬€傛牴鎹殏鍋滅洰鏍囷紝鑰佸勾浠g殑鍒嗗尯鍙兘涓嶈兘涓€娆℃殏鍋滄敹闆嗕腑琚鐞嗗畬锛孏1浼氬彂璧疯繛缁娆$殑娣峰悎鏀堕泦锛岀О涓烘贩鍚堟敹闆嗗懆鏈?Mixed Collection Cycle)銆侴1浼氳绠楁瘡娆″姞鍏ュ埌CSet涓殑鍒嗗尯鏁伴噺銆佹贩鍚堟敹闆嗚繘琛屾鏁帮紝骞朵笖鍦ㄤ笂娆$殑骞磋交浠f敹闆嗐€佷互鍙婃帴涓嬫潵鐨勬贩鍚堟敹闆嗕腑锛孏1浼氱‘瀹氫笅娆″姞鍏Set鐨勫垎鍖洪泦(Choose CSet)锛屽苟涓旂‘瀹氭槸鍚︾粨鏉熸贩鍚堟敹闆嗗懆鏈熴€?/p>

杞Щ澶辫触鐨勬媴淇濇満鍒?Full GC

杞Щ澶辫触鐨勬媴淇濇満鍒?Full GC

杞Щ澶辫触(Evacuation Failure)鏄寚褰揋1鏃犳硶鍦ㄥ爢绌洪棿涓敵璇锋柊鐨勫垎鍖烘椂锛孏1渚夸細瑙﹀彂鎷呬繚鏈哄埗锛屾墽琛屼竴娆TW寮忕殑銆佸崟绾跨▼鐨凢ull GC銆侳ull GC浼氬鏁村爢鍋氭爣璁版竻闄ゅ拰鍘嬬缉锛屾渶鍚庡皢鍙寘鍚函绮圭殑瀛樻椿瀵硅薄銆傚弬鏁?code class="mq-261">-XX:G1ReservePercent(榛樿10%)鍙互淇濈暀绌洪棿锛屾潵搴斿鏅嬪崌妯″紡涓嬬殑寮傚父鎯呭喌锛屾渶澶у崰鐢ㄦ暣鍫?0%锛屾洿澶т篃鏃犳剰涔夈€?/p>

G1鍦ㄤ互涓嬪満鏅腑浼氳Е鍙慒ull GC锛屽悓鏃朵細鍦ㄦ棩蹇椾腑璁板綍to-space-exhausted浠ュ強Evacuation Failure锛?/p>

  1. 浠庡勾杞讳唬鍒嗗尯鎷疯礉瀛樻椿瀵硅薄鏃讹紝鏃犳硶鎵惧埌鍙敤鐨勭┖闂插垎鍖?/p>

  2. 浠庤€佸勾浠e垎鍖鸿浆绉诲瓨娲诲璞℃椂锛屾棤娉曟壘鍒板彲鐢ㄧ殑绌洪棽鍒嗗尯

  3. 鍒嗛厤宸ㄥ瀷瀵硅薄鏃跺湪鑰佸勾浠f棤娉曟壘鍒拌冻澶熺殑杩炵画鍒嗗尯

鐢变簬G1鐨勫簲鐢ㄥ満鍚堝線寰€鍫嗗唴瀛橀兘姣旇緝澶э紝鎵€浠ull GC鐨勬敹闆嗕唬浠烽潪甯告槀璐碉紝搴旇閬垮厤Full GC鐨勫彂鐢熴€?/p>

绗簲绔?鎬荤粨

G1鏄竴娆鹃潪甯镐紭绉€鐨勫瀮鍦炬敹闆嗗櫒锛屼笉浠呴€傚悎鍫嗗唴瀛樺ぇ鐨勫簲鐢紝鍚屾椂涔熺畝鍖栦簡璋冧紭鐨勫伐浣溿€傞€氳繃涓昏鐨勫弬鏁板垵濮嬪拰鏈€澶у爢绌洪棿銆佷互鍙婃渶澶у蹇嶇殑GC鏆傚仠鐩爣锛屽氨鑳藉緱鍒颁笉閿欑殑鎬ц兘锛涘悓鏃讹紝鎴戜滑涔熺湅鍒癎1瀵瑰唴瀛樼┖闂寸殑娴垂杈冮珮锛屼絾閫氳繃**棣栧厛鏀堕泦灏藉彲鑳藉鐨勫瀮鍦?Garbage First)**鐨勮璁″師鍒欙紝鍙互鍙婃椂鍙戠幇杩囨湡瀵硅薄锛屼粠鑰岃鍐呭瓨鍗犵敤澶勪簬鍚堢悊鐨勬按骞炽€?/p>


鍙傝€冭祫鏂?/p>

[1] Charlie H, Monica B, Poonam P, Bengt R. Java Performance Companion
[2] 鍛ㄥ織鏄? 娣卞叆鐞嗚ВJVM铏氭嫙鏈?/p>

 https://www.cnblogs.com/jobbible/p/13476128.html

  

-娣卞叆鍘熺悊-  

   鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧    

璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣? class=



以上是关于璇﹁В JVM Garbage First(G1) 鍨冨溇鏀堕泦鍣?/h1>

的主要内容,如果未能解决你的问题,请参考以下文章

JVM7大垃圾回收器下篇G1(Garbage First)

JVM垃圾回收器之G1(Garbage First)--new

[JVM 相关] Java 新型垃圾回收器(Garbage First,G1)

The Garbage-First (G1) collector

Garbage First(G1)垃圾收集器

关于 G1(Garbage First)垃圾收集器