鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵

Posted 瓒h皥缂栫▼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵相关的知识,希望对你有一定的参考价值。

猬嗭笍 鍥剧墖鏉ユ簮浜庣綉缁?/span>


涔嬪墠涓婂鐨勬椂鍊欐湁杩欎釜涓€涓锛岃鍦ㄩ鍫傞噷鍚冮キ锛屽悆瀹屾妸椁愮洏绔蛋娓呯悊鐨勶紝鏄?C++ 绋嬪簭鍛橈紝鍚冨畬鐩存帴灏辫蛋鐨勶紝鏄?Java 绋嬪簭鍛樸€傪煠?/span>


纭疄锛屽湪 Java 鐨勪笘鐣岄噷锛屼技涔庢垜浠笉鐢ㄥ鍨冨溇鍥炴敹閭d箞鐨勪笓娉紝寰堝鍒濆鑰呬笉鎳?GC锛屼篃渚濈劧鑳藉啓鍑轰竴涓兘鐢ㄧ敋鑷宠繕涓嶉敊鐨勭▼搴忔垨绯荤粺銆備絾鍏跺疄杩欏苟涓嶄唬琛?Java 鐨?GC 灏变笉閲嶈銆傜浉鍙嶏紝瀹冩槸閭d箞鐨勯噸瑕佸拰澶嶆潅锛屼互鑷充簬鍑轰簡闂锛岄偅浜涘垵瀛﹁€呴櫎浜嗘墦寮€ GC 鏃ュ織锛岀湅鐫€涓€鍫?101鐨勫ぉ鏂囷紝鍟ヤ篃鍋氫笉浜嗐€傪煒?/span>


浠婂ぉ鎴戜滑灏变粠澶村埌灏惧畬鏁村湴鑱婁竴鑱?Java 鐨勫瀮鍦惧洖鏀躲€?br>


浠€涔堟槸鍨冨溇鍥炴敹



鍨冨溇鍥炴敹锛圙arbage Collection锛孏C锛夛紝椤惧悕鎬濅箟灏辨槸閲婃斁鍨冨溇鍗犵敤鐨勭┖闂达紝闃叉鍐呭瓨娉勯湶銆?/span>鏈夋晥鐨勪娇鐢ㄥ彲浠ヤ娇鐢ㄧ殑鍐呭瓨锛屽鍐呭瓨鍫嗕腑宸茬粡姝讳骸鐨勬垨鑰呴暱鏃堕棿娌℃湁浣跨敤鐨勫璞¤繘琛屾竻闄ゅ拰鍥炴敹銆?/span>


Java 璇█鍑烘潵涔嬪墠锛屽ぇ瀹堕兘鍦ㄦ嫾鍛界殑鍐?C 鎴栬€?C++ 鐨勭▼搴忥紝鑰屾鏃跺瓨鍦ㄤ竴涓緢澶х殑鐭涚浘锛孋++ 绛夎瑷€鍒涘缓瀵硅薄瑕佷笉鏂殑鍘诲紑杈熺┖闂达紝涓嶇敤鐨勬椂鍊欏張闇€瑕佷笉鏂殑鍘婚噴鏀炬帶浠讹紝鏃㈣鍐欐瀯閫犲嚱鏁帮紝鍙堣鍐欐瀽鏋勫嚱鏁帮紝寰堝鏃跺€欓兘鍦ㄩ噸澶嶇殑 allocated锛岀劧鍚庝笉鍋滅殑鏋愭瀯銆?/span>浜庢槸锛屾湁浜哄氨鎻愬嚭锛岃兘涓嶈兘鍐欎竴娈电▼搴忓疄鐜拌繖鍧楀姛鑳斤紝姣忔鍒涘缓锛岄噴鏀炬帶浠剁殑鏃跺€欏鐢ㄨ繖娈典唬鐮侊紝鑰屾棤闇€閲嶅鐨勪功鍐欏憿锛?/span>


1960骞达紝鍩轰簬 MIT 鐨?Lisp 棣栧厛鎻愬嚭浜嗗瀮鍦惧洖鏀剁殑姒傚康锛岃€岃繖鏃?Java 杩樻病鏈夊嚭涓栧憿锛?/span>鎵€浠ュ疄闄呬笂 GC 骞朵笉鏄疛ava鐨勪笓鍒╋紝GC 鐨勫巻鍙茶繙杩滃ぇ浜?Java 鐨勫巻鍙诧紒


鎬庝箞瀹氫箟鍨冨溇



鏃㈢劧鎴戜滑瑕佸仛鍨冨溇鍥炴敹锛岄鍏堟垜浠緱鎼炴竻妤氬瀮鍦剧殑瀹氫箟鏄粈涔堬紝鍝簺鍐呭瓨鏄渶瑕佸洖鏀剁殑銆?/span>


寮曠敤璁℃暟绠楁硶

寮曠敤璁℃暟绠楁硶锛圧eachability Counting锛夋槸閫氳繃鍦ㄥ璞″ご涓垎閰嶄竴涓┖闂存潵淇濆瓨璇ュ璞¤寮曠敤鐨勬鏁帮紙Reference Count锛夈€?/span>濡傛灉璇ュ璞¤鍏跺畠瀵硅薄寮曠敤锛屽垯瀹冪殑寮曠敤璁℃暟鍔?锛屽鏋滃垹闄ゅ璇ュ璞$殑寮曠敤锛岄偅涔堝畠鐨勫紩鐢ㄨ鏁板氨鍑?锛屽綋璇ュ璞$殑寮曠敤璁℃暟涓?鏃讹紝閭d箞璇ュ璞″氨浼氳鍥炴敹銆?/span>


String m = new String("jack");


鍏堝垱寤轰竴涓瓧绗︿覆锛岃繖鏃跺€?jack"鏈変竴涓紩鐢紝灏辨槸 m銆?/span>



鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵



鐒跺悗灏?m 璁剧疆涓?null锛岃繖鏃跺€?jack"鐨勫紩鐢ㄦ鏁板氨绛変簬0浜嗭紝鍦ㄥ紩鐢ㄨ鏁扮畻娉曚腑锛屾剰鍛崇潃杩欏潡鍐呭灏遍渶瑕佽鍥炴敹浜嗐€?/span>


m = null;


鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


寮曠敤璁℃暟绠楁硶鏄皢鍨冨溇鍥炴敹鍒嗘憡鍒版暣涓簲鐢ㄧ▼搴忕殑杩愯褰撲腑浜嗭紝鑰屼笉鏄湪杩涜鍨冨溇鏀堕泦鏃讹紝瑕佹寕璧锋暣涓簲鐢ㄧ殑杩愯锛岀洿鍒板鍫嗕腑鎵€鏈夊璞$殑澶勭悊閮界粨鏉熴€?/span>鍥犳锛岄噰鐢ㄥ紩鐢ㄨ鏁扮殑鍨冨溇鏀堕泦涓嶅睘浜庝弗鏍兼剰涔変笂鐨?Stop-The-World"鐨勫瀮鍦炬敹闆嗘満鍒躲€?/span>


鐪嬩技寰堢編濂斤紝浣嗘垜浠煡閬揓VM鐨勫瀮鍦惧洖鏀跺氨鏄?Stop-The-World"鐨勶紝閭f槸浠€涔堝師鍥犲鑷存垜浠渶缁堟斁寮冧簡寮曠敤璁℃暟绠楁硶鍛紵鐪嬩笅闈㈢殑渚嬪瓙銆?/span>

public class ReferenceCountingGC {
public Object instance;
public ReferenceCountingGC(String name){}}
public static void testGC(){
ReferenceCountingGC a = new ReferenceCountingGC("objA"); ReferenceCountingGC b = new ReferenceCountingGC("objB");
a.instance = b; b.instance = a;
a = null; b = null;}



1. 瀹氫箟2涓璞?/span>

2. 鐩镐簰寮曠敤
3. 缃┖鍚勮嚜鐨勫0鏄庡紩鐢?/span>


鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


鎴戜滑鍙互鐪嬪埌锛?/span>鏈€鍚庤繖2涓璞″凡缁忎笉鍙兘鍐嶈璁块棶浜嗭紝浣嗙敱浜庝粬浠浉浜掑紩鐢ㄧ潃瀵规柟锛屽鑷村畠浠殑寮曠敤璁℃暟姘歌繙閮戒笉浼氫负0锛岄€氳繃寮曠敤璁℃暟绠楁硶锛屼篃灏辨案杩滄棤娉曢€氱煡GC鏀堕泦鍣ㄥ洖鏀跺畠浠€?/span>


鍙揪鎬у垎鏋愮畻娉?/span>

鍙揪鎬у垎鏋愮畻娉曪紙Reachability Analysis锛夌殑鍩烘湰鎬濊矾鏄紝閫氳繃涓€浜涜绉颁负寮曠敤閾撅紙GC Roots锛?/strong>鐨勫璞′綔涓鸿捣鐐癸紝浠庤繖浜涜妭鐐瑰紑濮嬪悜涓嬫悳绱紝鎼滅储璧拌繃鐨勮矾寰勮绉颁负锛圧eference Chain)锛?/span>褰撲竴涓璞″埌 GC Roots 娌℃湁浠讳綍寮曠敤閾剧浉杩炴椂锛堝嵆浠?GC Roots 鑺傜偣鍒拌鑺傜偣涓嶅彲杈撅級锛屽垯璇佹槑璇ュ璞℃槸涓嶅彲鐢ㄧ殑銆?/span>



鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


閫氳繃鍙揪鎬х畻娉曪紝鎴愬姛瑙e喅浜嗗紩鐢ㄨ鏁版墍鏃犳硶瑙e喅鐨勯棶棰?鈥滃惊鐜緷璧栤€濓紝鍙浣犳棤娉曚笌 GC Root 寤虹珛鐩存帴鎴栭棿鎺ョ殑杩炴帴锛岀郴缁熷氨浼氬垽瀹氫綘涓哄彲鍥炴敹瀵硅薄銆傞偅杩欐牱灏卞紩鐢冲嚭浜嗗彟涓€涓棶棰橈紝鍝簺灞炰簬 GC Root锛?/span>


Java 鍐呭瓨鍖哄煙

鍦?Java 璇█涓紝鍙綔涓?GC Root 鐨勫璞″寘鎷互涓?绉嶏細

  • 铏氭嫙鏈烘爤锛堟爤甯т腑鐨勬湰鍦板彉閲忚〃锛変腑寮曠敤鐨勫璞?/span>

  • 鏂规硶鍖轰腑绫婚潤鎬佸睘鎬у紩鐢ㄧ殑瀵硅薄

  • 鏂规硶鍖轰腑甯搁噺寮曠敤鐨勫璞?/span>

  • 鏈湴鏂规硶鏍堜腑 JNI锛堝嵆涓€鑸鐨?Native 鏂规硶锛夊紩鐢ㄧ殑瀵硅薄


鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


铏氭嫙鏈烘爤锛堟爤甯т腑鐨勬湰鍦板彉閲忚〃锛変腑寮曠敤鐨勫璞?/span>


姝ゆ椂鐨?s锛堜笅闈唬鐮佷腑鐨剆锛夛紝鍗充负 GC Root锛屽綋s缃┖鏃讹紝localParameter 瀵硅薄涔熸柇鎺変簡涓?GC Root 鐨勫紩鐢ㄩ摼锛屽皢琚洖鏀躲€?/span>


public class StackLocalParameter { public StackLocalParameter(String name){}}
public static void testGC(){ StackLocalParameter s = new StackLocalParameter("localParameter"); s = null;}


鏂规硶鍖轰腑绫婚潤鎬佸睘鎬у紩鐢ㄧ殑瀵硅薄


s 涓?GC Root锛宻 缃负 null锛岀粡杩?GC 鍚庯紝s 鎵€鎸囧悜鐨?properties 瀵硅薄鐢变簬鏃犳硶涓?GC Root 寤虹珛鍏崇郴琚洖鏀躲€?/span>


鑰?m 浣滀负绫荤殑闈欐€佸睘鎬?/span>锛?/span>涔熷睘浜?GC Root锛宲arameter 瀵硅薄渚濈劧涓?GC root 寤虹珛鐫€杩炴帴锛屾墍浠ユ鏃?parameter 瀵硅薄骞朵笉浼氳鍥炴敹銆?/span>


public class MethodAreaStaicProperties { public static MethodAreaStaicProperties m; public MethodAreaStaicProperties(String name){}}
public static void testGC(){ MethodAreaStaicProperties s = new MethodAreaStaicProperties("properties"); s.m = new MethodAreaStaicProperties("parameter"); s = null;}


鏂规硶鍖轰腑甯搁噺寮曠敤鐨勫璞?/span>


m 鍗充负鏂规硶鍖轰腑鐨勫父閲忓紩鐢?/span>锛屼篃涓?GC Root锛宻 缃负 null 鍚庯紝final 瀵硅薄涔熶笉浼氬洜娌℃湁涓?GC Root 寤虹珛鑱旂郴鑰岃鍥炴敹銆?/span>


public class MethodAreaStaicProperties { public static final MethodAreaStaicProperties m = MethodAreaStaicProperties("final"); public MethodAreaStaicProperties(String name){}}
public static void testGC(){ MethodAreaStaicProperties s = new MethodAreaStaicProperties("staticProperties"); s = null;}


鏈湴鏂规硶鏍堜腑寮曠敤鐨勫璞?/span>


浠讳綍 Native 鎺ュ彛閮戒細浣跨敤鏌愮鏈湴鏂规硶鏍堬紝瀹炵幇鐨勬湰鍦版柟娉曟帴鍙f槸浣跨敤 C 杩炴帴妯″瀷鐨勮瘽锛岄偅涔堝畠鐨勬湰鍦版柟娉曟爤灏辨槸 C 鏍堛€?/span>褰撶嚎绋嬭皟鐢?Java 鏂规硶鏃讹紝铏氭嫙鏈轰細鍒涘缓涓€涓柊鐨勬爤甯у苟鍘嬪叆 Java 鏍堛€?/span>鐒惰€屽綋瀹冭皟鐢ㄧ殑鏄湰鍦版柟娉曟椂锛岃櫄鎷熸満浼氫繚鎸?Java 鏍堜笉鍙橈紝涓嶅啀鍦ㄧ嚎绋嬬殑 Java 鏍堜腑鍘嬪叆鏂扮殑甯э紝铏氭嫙鏈哄彧鏄畝鍗曞湴鍔ㄦ€佽繛鎺ュ苟鐩存帴璋冪敤鎸囧畾鐨勬湰鍦版柟娉曘€?/span>


鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵



鎬庝箞鍥炴敹鍨冨溇



鍦ㄧ‘瀹氫簡鍝簺鍨冨溇鍙互琚洖鏀跺悗锛屽瀮鍦炬敹闆嗗櫒瑕佸仛鐨勪簨鎯呭氨鏄紑濮嬭繘琛屽瀮鍦惧洖鏀讹紝浣嗘槸杩欓噷闈㈡秹鍙婂埌涓€涓棶棰樻槸锛?/span>濡備綍楂樻晥鍦拌繘琛屽瀮鍦惧洖鏀讹紵


鐢变簬Java铏氭嫙鏈鸿鑼冨苟娌℃湁瀵瑰浣曞疄鐜板瀮鍦炬敹闆嗗櫒鍋氬嚭鏄庣‘鐨勮瀹氾紝鍥犳鍚勪釜鍘傚晢鐨勮櫄鎷熸満鍙互閲囩敤涓嶅悓鐨勬柟寮忔潵瀹炵幇鍨冨溇鏀堕泦鍣紝杩欓噷鎴戜滑璁ㄨ鍑犵甯歌鐨勫瀮鍦炬敹闆嗙畻娉曠殑鏍稿績鎬濇兂銆?/span>


鏍囪 --- 娓呴櫎绠楁硶

鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


鏍囪娓呴櫎绠楁硶锛圡ark-Sweep锛夋槸鏈€鍩虹鐨勪竴绉嶅瀮鍦惧洖鏀剁畻娉曪紝瀹冨垎涓?閮ㄥ垎锛屽厛鎶婂唴瀛樺尯鍩熶腑鐨勮繖浜涘璞¤繘琛屾爣璁帮紝鍝簺灞炰簬鍙洖鏀舵爣璁板嚭鏉ワ紝鐒跺悗鎶婅繖浜涘瀮鍦炬嫀鍑烘潵娓呯悊鎺?/span>銆傚氨鍍忎笂鍥句竴鏍凤紝娓呯悊鎺夌殑鍨冨溇灏卞彉鎴愭湭浣跨敤鐨勫唴瀛樺尯鍩燂紝绛夊緟琚啀娆′娇鐢ㄣ€?/h1>


娑涘0渚濇棫娉細鏍囪鍨冨溇瀵硅薄鏄剧劧閲囩敤鐨勫氨鏄笂鏂囦腑璇寸殑鍙揪鎬у垎鏋愩€?/p>


杩欓€昏緫鍐嶆竻鏅颁笉杩囦簡锛屽苟涓斾篃寰堝ソ鎿嶄綔锛屼絾瀹冨瓨鍦ㄤ竴涓緢澶х殑闂锛岄偅灏辨槸鍐呭瓨纰庣墖銆?/h1>


涓婂浘涓瓑鏂瑰潡鐨勫亣璁炬槸 2M锛屽皬涓€浜涚殑鏄?1M锛屽ぇ涓€浜涚殑鏄?4M銆傜瓑鎴戜滑鍥炴敹瀹岋紝鍐呭瓨灏变細鍒囨垚浜嗗緢澶氭銆傛垜浠煡閬?span class="mq-233">寮€杈熷唴瀛樼┖闂存椂锛岄渶瑕佺殑鏄繛缁殑鍐呭瓨鍖哄煙锛岃繖鏃跺€欐垜浠渶瑕佷竴涓?2M鐨勫唴瀛樺尯鍩燂紝鍏朵腑鏈?涓?1M 鏄病娉曠敤鐨勩€傝繖鏍峰氨瀵艰嚧锛屽叾瀹炴垜浠湰韬繕鏈夎繖涔堝鐨勫唴瀛樼殑锛屼絾鍗寸敤涓嶄簡銆?/h1>


澶嶅埗绠楁硶

鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


澶嶅埗绠楁硶锛圕opying锛夋槸鍦ㄦ爣璁版竻闄ょ畻娉曚笂婕斿寲鑰屾潵锛岃В鍐虫爣璁版竻闄ょ畻娉曠殑鍐呭瓨纰庣墖闂銆傚畠灏嗗彲鐢ㄥ唴瀛樻寜瀹归噺鍒掑垎涓哄ぇ灏忕浉绛夌殑涓ゅ潡锛屾瘡娆″彧浣跨敤鍏朵腑鐨勪竴鍧椼€傚綋杩欎竴鍧楃殑鍐呭瓨鐢ㄥ畬浜嗭紝灏?/span>灏嗚繕瀛樻椿鐫€鐨勫璞″鍒跺埌鍙﹀涓€鍧椾笂闈?/span>锛?/span>鐒跺悗鍐嶆妸宸蹭娇鐢ㄨ繃鐨勫唴瀛樼┖闂翠竴娆℃竻鐞嗘帀銆備繚璇佷簡鍐呭瓨鐨勮繛缁彲鐢紝鍐呭瓨鍒嗛厤鏃朵篃灏变笉鐢ㄨ€冭檻鍐呭瓨纰庣墖绛夊鏉傛儏鍐碉紝閫昏緫娓呮櫚锛岃繍琛岄珮鏁堛€?/span>


涓婇潰鐨勫浘寰堟竻妤氾紝涔熷緢鏄庢樉鐨勬毚闇蹭簡鍙︿竴涓棶棰橈紝鍚堢潃鎴戣繖140骞崇殑澶т笁鎴匡紝鍙兘褰?0骞崇背鐨勫皬涓ゆ埧鏉ヤ娇锛?/span>浠d环瀹炲湪澶珮銆?/span>


鏍囪鏁寸悊绠楁硶

鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


鏍囪鏁寸悊绠楁硶锛圡ark-Compact锛夋爣璁拌繃绋嬩粛鐒朵笌鏍囪 --- 娓呴櫎绠楁硶涓€鏍凤紝浣嗗悗缁楠や笉鏄洿鎺ュ鍙洖鏀跺璞¤繘琛屾竻鐞嗭紝鑰屾槸璁╂墍鏈夊瓨娲荤殑瀵硅薄閮藉悜涓€绔Щ鍔?/span>锛?/span>鍐嶆竻鐞嗘帀绔竟鐣屼互澶栫殑鍐呭瓨鍖哄煙銆?/span>



鍒嗕唬鏀堕泦绠楁硶鍒嗕唬鏀堕泦绠楁硶锛圙enerational Collection锛変弗鏍兼潵璇村苟涓嶆槸涓€绉嶆€濇兂鎴栫悊璁猴紝鑰屾槸铻嶅悎涓婅堪3绉嶅熀纭€鐨勭畻娉曟€濇兂锛岃€屼骇鐢熺殑閽堝涓嶅悓鎯呭喌鎵€閲囩敤涓嶅悓绠楁硶鐨勪竴濂楃粍鍚堟嫵銆?/span>


瀵硅薄瀛樻椿鍛ㄦ湡鐨勪笉鍚屽皢鍐呭瓨鍒掑垎涓哄嚑鍧椼€?/span>涓€鑸槸鎶?Java 鍫嗗垎涓烘柊鐢熶唬鍜岃€佸勾浠o紝杩欐牱灏卞彲浠ユ牴鎹悇涓勾浠g殑鐗圭偣閲囩敤鏈€閫傚綋鐨勬敹闆嗙畻娉曘€?/span>


鍦ㄦ柊鐢熶唬涓紝姣忔鍨冨溇鏀堕泦鏃堕兘鍙戠幇鏈夊ぇ鎵瑰璞℃鍘伙紝鍙湁灏戦噺瀛樻椿锛岄偅灏遍€夌敤澶嶅埗绠楁硶锛?/span>鍙渶瑕佷粯鍑哄皯閲忓瓨娲诲璞$殑澶嶅埗鎴愭湰灏卞彲浠ュ畬鎴愭敹闆?/span>銆?/span>


鑰岃€佸勾浠d腑鍥犱负瀵硅薄瀛樻椿鐜囬珮銆佹病鏈夐澶栫┖闂村瀹冭繘琛屽垎閰嶆媴淇濓紝灏卞繀椤讳娇鐢ㄦ爣璁?娓呯悊鎴栬€呮爣璁?--- 鏁寸悊绠楁硶鏉ヨ繘琛屽洖鏀躲€俿o锛屽彟涓€涓棶棰樻潵浜嗭紝閭?/span>鍐呭瓨鍖哄煙鍒板簳琚垎涓哄摢鍑犲潡锛屾瘡涓€鍧楀張鏈変粈涔堢壒鍒€傚悎浠€涔堢畻娉曞憿锛?/span>



鍐呭瓨妯″瀷涓庡洖鏀剁瓥鐣?/span>



鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵


Java 鍫嗭紙Java Heap锛夋槸JVM鎵€绠$悊鐨勫唴瀛樹腑鏈€澶х殑涓€鍧楋紝鍫嗗張鏄瀮鍦炬敹闆嗗櫒绠$悊鐨勪富瑕佸尯鍩燂紝杩欓噷鎴戜滑涓昏鍒嗘瀽涓€涓?Java 鍫嗙殑缁撴瀯銆?/span>


Java 鍫嗕富瑕佸垎涓?涓尯鍩?骞磋交浠?/strong>涓?span>鑰佸勾浠?/strong>锛屽叾涓勾杞讳唬鍙堝垎 Eden 鍖?/strong>鍜?Survivor 鍖?/strong>锛屽叾涓?Survivor 鍖哄張鍒?From 鍜?span> To 2涓尯銆?/span>


鍙兘杩欐椂鍊欏ぇ瀹朵細鏈夌枒闂紝涓轰粈涔堥渶瑕?nbsp;Survivor 鍖猴紝涓轰粈涔?span class="mq-312">Survivor 杩樿鍒?涓尯銆?/span>涓嶇潃鎬ワ紝鎴戜滑浠庡ご鍒板熬锛岀湅鐪嬪璞″埌搴曟槸鎬庝箞鏉ョ殑锛岃€屽畠鍙堟槸鎬庝箞娌$殑銆?/span>


Eden 鍖?/span>

IBM 鍏徃鐨勪笓涓氱爺绌惰〃鏄庯紝鏈夊皢杩?8%鐨勫璞℃槸鏈濈敓澶曟锛屾墍浠ラ拡瀵硅繖涓€鐜扮姸锛屽ぇ澶氭暟鎯呭喌涓嬶紝瀵硅薄浼氬湪鏂扮敓浠?Eden 鍖轰腑杩涜鍒嗛厤锛屽綋 Eden 鍖烘病鏈夎冻澶熺┖闂磋繘琛屽垎閰嶆椂锛岃櫄鎷熸満浼氬彂璧蜂竴娆?Minor GC锛孧inor GC 鐩告瘮 Major GC 鏇撮绻侊紝鍥炴敹閫熷害涔熸洿蹇€?/span>


閫氳繃 Minor GC 涔嬪悗锛孍den 浼氳娓呯┖锛孍den 鍖轰腑缁濆ぇ閮ㄥ垎瀵硅薄浼氳鍥炴敹锛岃€岄偅浜涙棤闇€鍥炴敹鐨勫瓨娲诲璞★紝灏嗕細杩涘埌 Survivor 鐨?From 鍖猴紙鑻?From 鍖轰笉澶燂紝鍒欑洿鎺ヨ繘鍏?Old 鍖猴級銆?/span>


Survivor 鍖?/span>

Survivor 鍖虹浉褰撲簬鏄?Eden 鍖哄拰 Old 鍖虹殑涓€涓紦鍐?/span>锛岀被浼间簬鎴戜滑浜ら€氱伅涓殑榛勭伅銆係urvivor 鍙堝垎涓?涓尯锛屼竴涓槸 From 鍖猴紝涓€涓槸 To 鍖恒€傛瘡娆℃墽琛?Minor GC锛屼細灏?Eden 鍖哄拰 From 瀛樻椿鐨勫璞℃斁鍒?Survivor 鐨?To 鍖猴紙濡傛灉 To 鍖轰笉澶燂紝鍒欑洿鎺ヨ繘鍏?Old 鍖猴級銆?/span>


涓哄暐闇€瑕侊紵

涓嶅氨鏄柊鐢熶唬鍒拌€佸勾浠d箞锛岀洿鎺?Eden 鍒?Old 涓嶅ソ浜嗗悧锛屼负鍟ヨ杩欎箞澶嶆潅銆傛兂鎯冲鏋滄病鏈?Survivor 鍖猴紝Eden 鍖烘瘡杩涜涓€娆?Minor GC锛屽瓨娲荤殑瀵硅薄灏变細琚€佸埌鑰佸勾浠o紝鑰佸勾浠e緢蹇氨浼氳濉弧銆?/span>


鑰屾湁寰堝瀵硅薄铏界劧涓€娆?Minor GC 娌℃湁娑堢伃锛屼絾鍏跺疄涔熷苟涓嶄細韫﹁范澶氫箙锛屾垨璁哥浜屾锛岀涓夋灏遍渶瑕佽娓呴櫎銆?/span>杩欐椂鍊欑Щ鍏ヨ€佸勾鍖猴紝寰堟槑鏄句笉鏄竴涓槑鏅虹殑鍐冲畾銆?/span>


鎵€浠ワ紝Survivor 鐨勫瓨鍦ㄦ剰涔夊氨鏄噺灏戣閫佸埌鑰佸勾浠g殑瀵硅薄锛岃繘鑰屽噺灏?Major GC 鐨勫彂鐢熴€係urvivor 鐨勯绛涢€変繚璇侊紝鍙湁缁忓巻16娆?Minor GC 杩樿兘鍦ㄦ柊鐢熶唬涓瓨娲荤殑瀵硅薄锛屾墠浼氳閫佸埌鑰佸勾浠c€?/span>


涓哄暐闇€瑕佷咯锛?/span>

璁剧疆涓や釜 Survivor 鍖烘渶澶х殑濂藉灏辨槸瑙e喅鍐呭瓨纰庣墖鍖栥€?/span>


鎴戜滑鍏堝亣璁句竴涓嬶紝Survivor 濡傛灉鍙湁涓€涓尯鍩熶細鎬庢牱銆侻inor GC 鎵ц鍚庯紝Eden 鍖鸿娓呯┖浜嗭紝瀛樻椿鐨勫璞℃斁鍒颁簡 Survivor 鍖猴紝鑰?/span>涔嬪墠 Survivor 鍖轰腑鐨勫璞?/span>锛?/span>鍙兘涔?/span>鏈変竴浜?/span>鏄渶瑕佽娓呴櫎鐨?/span>銆?/span>


闂鏉ヤ簡锛岃繖鏃跺€欐垜浠€庝箞娓呴櫎瀹冧滑锛熷湪杩欑鍦烘櫙涓嬶紝鎴戜滑鍙兘鏍囪娓呴櫎锛岃€屾垜浠煡閬撴爣璁版竻闄ゆ渶澶х殑闂灏辨槸鍐呭瓨纰庣墖锛屽湪鏂扮敓浠h繖绉?/span>缁忓父浼氭秷浜$殑鍖哄煙锛?/span>閲囩敤鏍囪娓呴櫎蹇呯劧浼氳鍐呭瓨浜х敓涓ラ噸鐨勭鐗囧寲銆?/span>


鍥犱负 Survivor 鏈?涓尯鍩燂紝鎵€浠ユ瘡娆?Minor GC锛屼細灏嗕箣鍓?Eden 鍖哄拰 From 鍖轰腑鐨勫瓨娲诲璞″鍒跺埌 To 鍖哄煙銆?/span>绗簩娆?Minor GC 鏃讹紝From 涓?To 鑱岃矗鍏戞崲锛岃繖鏃跺€欎細灏?Eden 鍖哄拰 To 鍖轰腑鐨勫瓨娲诲璞″啀澶嶅埗鍒?From 鍖哄煙锛屼互姝ゅ弽澶嶃€?/span>


杩欑鏈哄埗鏈€澶х殑濂藉灏辨槸锛屾暣涓繃绋嬩腑锛?/span>姘歌繙鏈変竴涓?nbsp;Survivor space 鏄┖鐨勶紝鍙︿竴涓潪绌虹殑 Survivor space 鏄棤纰庣墖鐨?/span>銆傞偅涔堬紝Survivor 涓轰粈涔堜笉鍒嗘洿澶氬潡鍛紵姣旀柟璇村垎鎴愪笁涓€佸洓涓€佷簲涓?鏄剧劧锛屽鏋?Survivor 鍖哄啀缁嗗垎涓嬪幓锛屾瘡涓€鍧楃殑绌洪棿灏变細姣旇緝灏忥紝瀹规槗瀵艰嚧 Survivor 鍖烘弧锛屼袱鍧?Survivor 鍖哄彲鑳芥槸缁忚繃鏉冭 涔嬪悗鐨勬渶浣虫柟妗堛€?/span>


Old 鍖?/span>

鑰佸勾浠e崰鎹潃2/3鐨勫爢鍐呭瓨绌洪棿锛屽彧鏈夊湪 Major GC 鐨勬椂鍊欐墠浼氳繘琛屾竻鐞嗭紝姣忔 GC 閮戒細瑙﹀彂鈥淪top-The-World鈥濄€傚唴瀛樿秺澶э紝STW 鐨勬椂闂翠篃瓒婇暱锛屾墍浠ュ唴瀛樹篃涓嶄粎浠呮槸瓒婂ぇ灏辫秺濂姐€傜敱浜庡鍒剁畻娉曞湪瀵硅薄瀛樻椿鐜囪緝楂樼殑鑰佸勾浠d細杩涜寰堝娆$殑澶嶅埗鎿嶄綔锛屾晥鐜囧緢浣?/span>锛屾墍浠ヨ€佸勾浠h繖閲岄噰鐢ㄧ殑鏄爣璁?--- 鏁寸悊绠楁硶銆?/span>


闄や簡涓婅堪鎵€璇达紝鍦ㄥ唴瀛樻媴淇濇満鍒朵笅锛屾棤娉曞畨缃殑瀵硅薄浼氱洿鎺ヨ繘鍒拌€佸勾浠o紝浠ヤ笅鍑犵鎯呭喌涔熶細杩涘叆鑰佸勾浠c€?/span>


澶у璞?/span>

澶у璞℃寚闇€瑕佸ぇ閲忚繛缁唴瀛樼┖闂寸殑瀵硅薄锛岃繖閮ㄥ垎瀵硅薄涓嶇鏄笉鏄€滄湞鐢熷姝烩€濓紝閮戒細鐩存帴杩涘埌鑰佸勾浠?/span>銆傝繖鏍峰仛涓昏鏄负浜嗛伩鍏嶅湪 Eden 鍖哄強2涓?Survivor 鍖轰箣闂村彂鐢熷ぇ閲忕殑鍐呭瓨澶嶅埗銆傚綋浣犵殑绯荤粺鏈夐潪甯稿鈥滄湞鐢熷姝烩€濈殑澶у璞℃椂锛屽緱娉ㄦ剰浜嗐€?/span>


闀挎湡瀛樻椿瀵硅薄

铏氭嫙鏈虹粰姣忎釜瀵硅薄瀹氫箟浜嗕竴涓璞″勾榫勶紙Age锛夎鏁板櫒銆?/span>姝e父鎯呭喌涓嬪璞′細涓嶆柇鐨勫湪 Survivor 鐨?From 鍖轰笌 To 鍖轰箣闂寸Щ鍔紝瀵硅薄鍦?Survivor 鍖轰腑姣忕粡鍘嗕竴娆?Minor GC锛屽勾榫勫氨澧炲姞1宀併€?/span>褰撳勾榫勫鍔犲埌15宀佹椂锛岃繖鏃跺€欏氨浼氳杞Щ鍒拌€佸勾浠c€?/span>褰撶劧锛岃繖閲岀殑15锛孞VM 涔熸敮鎸佽繘琛岀壒娈婅缃€?/span>


鍔ㄦ€佸璞″勾榫?/span>

铏氭嫙鏈哄苟涓嶉噸瑙嗚姹傚璞″勾榫勫繀椤诲埌15宀侊紝鎵嶄細鏀惧叆鑰佸勾鍖猴紝濡傛灉 Survivor 绌洪棿涓浉鍚屽勾榫勬墍鏈夊璞″ぇ灏忕殑鎬诲悎澶т簬 Survivor 绌洪棿鐨勪竴鍗婏紝骞撮緞澶т簬绛変簬璇ュ勾榫勭殑瀵硅薄灏卞彲浠ョ洿鎺ヨ繘鍘昏€佸勾鍖猴紝鏃犻渶绛変綘鈥滄垚骞粹€濄€?/span>


杩欏叾瀹炴湁鐐圭被浼间簬璐熻浇鍧囪 锛岃疆璇㈡槸璐熻浇鍧囪 鐨勪竴绉嶏紝淇濊瘉姣忓彴鏈哄櫒閮藉垎寰楀悓鏍风殑璇锋眰銆?/span>鐪嬩技寰堝潎琛★紝浣嗘瘡鍙版満鐨勭‖浠朵笉閫氾紝鍋ュ悍鐘跺喌涓嶅悓锛屾垜浠繕鍙互鍩轰簬姣忓彴鏈烘帴鍙楃殑璇锋眰鏁帮紝鎴栨瘡鍙版満鐨勫搷搴旀椂闂寸瓑锛屾潵璋冩暣鎴戜滑鐨勮礋杞藉潎琛$畻娉曘€?/span>


鏈枃閮ㄥ垎鍐呭鍙傝€冭嚜涔︾睄锛氥€婃繁鍏ョ悊瑙ava铏氭嫙鏈恒€嬨€?/span>



姣忓懆涓€鎺?/span>

绗竴鏃堕棿鑾峰緱涓嬫湡鍒嗕韩


瓒h皥缂栫▼

鍏虫敞鎴戯紝璁╀綘杞绘澗瀛︾紪绋?/span>


以上是关于鍨冨溇鍥炴敹鍣細浣犳槸浠€涔堝瀮鍦撅紵的主要内容,如果未能解决你的问题,请参考以下文章

[绉戞櫘]ES6涓€浜涗笉甯歌鐨勫皬鐭ヨ瘑

Java鍘嗗彶[鍙蹭笂鏈€璇︾粏鐨勯槓杩颁簡Java鍙戝睍鐨勫巻鍙茶繃绋媇

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

鏈哄櫒瀛︿範锛?3-鍨冨溇閭欢鍒嗙被2

鍨冨溇浠g爜涔﹀啓鍑嗗垯锛堟湁鎰忔€濓級

DevOps 浼氳浣犲け涓氬悧锛?| Linux 涓浗