鍓嶅嚑澶╁湪缂栧啓浠g爜楂樹寒鑴氭湰鐨勬椂鍊欙紝闂簡鐡跺瓙涓€涓棶棰橈紝灏辨槸鍦ㄥ惊鐜噷澶勭悊鍒犻櫎DOM鍏冪礌鐨勬椂鍊欙紝浼氬姩鎬佹敼鍙楴odeList鐨刲ength锛屾墍浠ユ祴璇曡涔咃紝鏈€鍚庡彂鐜版槸杩欎釜闂锛岀媯鏅曘€備絾鏄湡闂磋皥鍒颁簡涓€涓叧浜巖emoveChild鐨勬椂鍊欏湪IE涓嬫棤娉曞洖鏀跺唴瀛樼殑娉勬紡闂锛屼粬灞曠ず浜嗕竴涓狤XT閲岄拡瀵笽E浣跨敤鐨勬柟娉曪細
var div=document.getElementById("div"); var first=div.firstChild,next=first; while(next){ var d=document.createElement("div"); d.appendChild(next); d.innerHTML=""; next=div.firstChild; } /////////////////////////////////////////////////// //绠€鍗曠殑removeChild鏂瑰紡锛?var div=document.getElementById("div"); var first=div.firstChild,next=first; while(next){ next.parentNode.removeChild(next); next=div.firstChild; }
浣嗘槸缁忚繃浣跨敤Drip宸ュ叿锛堟祴璇旾E鏄惁鍐呭瓨娉勬紡鐨勫伐鍏凤紝锛夛紝娴嬭瘯杩樻槸瀛樺湪鍐呭瓨娉勬紡鐨勯棶棰橈紝浣嗘槸浣跨敤IE JS Leaks Detector鍗村暐涔熸娴嬩笉鍑烘潵锛堝叏閮ㄧ殑娴嬭瘯閮芥娴嬩笉鍑烘潵锛屽氨杩炵綉涓婇兘鍚规崸鐨勫唴瀛樻硠婕忕殑鏂瑰紡涔熸娴嬩笉鍑烘潵锛夛紝杩樻湁浣跨敤浜嗚瘽璇存槸Drip鐨勫寮虹増鐨剆IEve()锛屼篃娴嬭瘯涓嶅嚭鏉ャ€傛棦鐒惰繖鏍凤紝閭e氨鏆備笖淇′换Drip鍚с€備笅闈㈠嚑绉嶄紶璇翠腑鐨勫唴瀛樻硠婕忕殑鏂瑰紡閮芥槸鍦―rip涓嬫祴璇曠殑銆?
鍦ㄥ紑濮嬭杩颁箣鍓嶏紝鍏堝ぇ姒備簡瑙d竴涓媕avascript鐨凣C鏈哄埗锛?/p>
鍨冨溇鍥炴敹杩涚▼灏濊瘯鎺ㄦ柇浣曟椂鍙互瀹夊叏鍦板洖鏀朵笉鍐嶄娇鐢ㄧ殑鍙橀噺锛岄€氬父鏄€氳繃鍒ゅ畾绋嬪簭鏄惁鑳藉閫氳繃鍙橀噺涔嬮棿褰㈡垚鐨勫紩鐢ㄧ綉缁滃埌杈捐鍙橀噺銆傚綋纭俊鍙橀噺鏄笉鍙揪鐨勶紝灏卞湪瀹冧笂闈㈡爣涓婂彲浠ュ洖鏀剁殑璁板彿锛屽苟涓斿湪鍥炴敹鍣ㄧ殑涓嬩竴娆℃竻鐞嗕腑锛堝彲鑳藉湪鏈潵鐨勪换鎰忔椂鍒?閲婃斁鐩稿叧鐨勫唴瀛樸€?/p>
涔熷氨鏄锛屽瀮鍦惧洖鏀舵満鍒朵細瀹氭椂鐨勬鏌ョ▼搴忎腑鐨勫璞★紝鏌ョ湅瀹冩槸鍚﹁窡鍒殑瀵硅薄涔嬮棿宸茬粡瀹屽叏鏂紑浜嗗紩鐢ㄩ摼鑰屸€滃鍗曚竴浜衡€濓紝杩欐椂锛屽瀮鍦惧洖鏀舵満鍒跺氨浼氬洖鏀惰繖涓璞$殑鍐呭瓨锛屽惁鍒欙紝灏嗕笉浼氬洖鏀躲€傛墍浠ヨ锛屽璞″湪浣跨敤瀹屼簡涔嬪悗锛屽氨搴旇琚洖鏀跺唴瀛橈紝鑰屼笉鏄竴鐩村崰鐢ㄧ潃鍐呭瓨涓嶆斁锛屽鑷存祻瑙堝櫒鐨勫唴瀛樹娇鐢ㄩ噺鑺傝妭椋欏崌銆?
绗竴绉嶏細鏃㈢劧涓婇潰璋堝埌浜嗗叧浜巖emoveChild锛岄偅灏变粠瀹冨紑濮嬪惂锛岄€氳繃Drip娴嬭瘯锛岀畝鍗曠殑浣跨敤removeChild鍒犻櫎瀛愯妭鐐圭殑鏂瑰紡纭疄瀛樺湪鍐呭瓨娉勬紡锛屼絾鏄娇鐢ㄤ簡涓婇潰EXT浣跨敤鐨勬柟寮忥紝涔熻繕鏄瓨鍦ㄣ€傜粡杩囦竴鐣悳绱紝鏈夋枃绔犺闇€瑕佹竻闄よ妭鐐圭殑鍏ㄩ儴灞炴€ф潵瀹炵幇鍐呭瓨鐨勬纭洖鏀讹紝閭e氨杩涜浜嗕笅闈㈢殑娴嬭瘯銆傜粨鏋滈€氳繃灏嗚妭鐐圭殑灞炴€ч兘delete鎺変箣鍚庯紝Drip鏄剧ず娌℃湁鍐呭瓨娉勬紡浜嗐€?
var div=document.getElementById("div"); var first=div.firstChild,next=first; while(next){ div.removeChild(next); for(var k in next){ delete next[k]; } next=div.firstChild; }
绗簩绉嶏細灏嗕竴涓狣OM瀵硅薄鍜屼竴涓狫S瀵硅薄鐩镐簰鎴愪负瀵规柟鐨勫睘鎬с€傚浜庤繖鐐癸紝IE瀹樻柟涔熼兘鏈夎娉曪細鍦↖E6涓紝瀵逛簬javascript object鍐呴儴锛宩script浣跨敤鐨勬槸mark-and-sweep绠楁硶锛岃€屽浜巎avascript object涓庡閮╫bject(鍖呮嫭native object鍜寁bscript object绛夌瓑)鐨勫紩鐢ㄦ椂锛孖E 6浣跨敤鐨勬墠鏄鏁板櫒鐨勭畻娉曘€備篃灏辨槸璇达紝IE 6瀵逛簬绾补鐨凷cript Objects闂寸殑Circular References鏄彲浠ユ纭鐞嗙殑锛屽彲鎯滃畠澶勭悊涓嶄簡鐨勬槸JScript涓嶯ative Object(渚嬪Dom銆丄ctiveX Object)涔嬮棿鐨凜ircular References銆傛墍浠ワ紝褰撴垜浠嚭鐜癗ative瀵硅薄(渚嬪Dom銆丄ctiveX Object)涓嶫avascript瀵硅薄闂寸殑寰幆寮曠敤鏃讹紝鍐呭瓨娉勯湶鐨勯棶棰樺氨鍑虹幇浜嗐€傚綋鐒讹紝杩欎釜bug鍦↖E 7涓凡缁忚淇浜嗐€傦紙Fuck锛岄毦鎬垜鐢―rip娴嬭瘯涓嶅嚭鏉ワ紙绯荤粺鏄疘E8鐨勫唴鏍革級锛夈€備笅闈㈡槸鎴戠殑涓€涓祴璇曪細
function Encapsulator(element){ this.elementReference = element; element.expandoProperty = this; } function SetupLeak2(){ var obj=new Encapsulator(document.getElementById("test")); document.body.removeChild(document.getElementById("test")); //alert(document.getElementById("test").expandoProperty); 鍑虹幇閿欒 //璇存槑浠巈lement.expandoProperty ---> obj鐨勫紩鐢ㄥ凡缁忔柇寮€浜? //浣嗘槸浠巓bj.elementReference鍒癳lement鐨勫紩鐢ㄤ緷鐒跺瓨鍦紝 //杩欐牱鐨勮瘽鍦↖E6涓媏lement灏辨棤娉曞洖鏀跺唴瀛橈紝浣嗘槸鍏朵粬娴忚鍣ㄧ殑GC鏈哄埗閮戒細寰堝ソ鐨勫鐞嗕簡杩欎釜闂銆? document.body.appendChild(obj.elementReference); }
绗笁绉嶏細灏嗕簨浠跺鐞嗗嚱鏁版斁鍦ㄥ畾涔夊畠鐨勫嚱鏁扮殑鍐呴儴銆傝繖绉嶆儏鍐典箣鍓嶅氨鐪嬪埌杩囷紝鍥炴兂涓嬭嚜宸变互鍓嶇紪鍐檍s鐨勬柟寮忥細澶栧寘涓€涓嚜鎵ц鍑芥暟锛岄噷闈㈠畾涔夐棴鍖呭唴鐨勫彉閲忓拰鍔熻兘鍑芥暟锛屼篃涓嶄箯瀵逛簨浠跺鐞嗙▼搴忕殑澶勭悊銆傝繖鏍锋槸鍚︿細閫犳垚IE涓嬬殑鍐呭瓨娉勬紡鍛紵涓嬮潰鏄袱涓祴璇曠▼搴忥細
var test=function(){ var div=document.getElementById("test"); var i=0; while((i++) < 20){ (function(index){ var o=document.createElement("p"); o.innerHTML="AAA"; o.onclick=function(){ alert("haha,leap"); } div.appendChild(o); o.onclick=null; div.removeChild(o); })(i); } }
function addEvent(){ var div=document.getElementById("event"); div.onclick=function(){ this.parentNode.removeChild(this); } }
涓婇潰鐨勪竴娈电▼搴忎篃鏄粠缃戜笂鎽樺綍涓嬫潵鍋氭祴璇曠殑锛屽湪闂寘涓姩鎬佺敓鎴愪竴涓猟iv鍏冪礌锛屽苟缁欏畠娣诲姞浜嬩欢锛屼簨浠跺鐞嗙▼搴忓啓鍦ㄩ棴鍖呴噷闈紝涔熷氨鏄唴娑靛湪test鍑芥暟閲岄潰锛屽彲鏄湪removeChild鐨勬椂鍊欙紝Drip涓嬫樉绀鸿繕鏄唴瀛樻硠婕忎簡锛屽嵆浣挎槸鎶婂畠鐨刼nclick灞炴€ц缃负null涔熶笉琛屻€傜浜屼釜娴嬭瘯绋嬪簭涓紝鍦ㄤ簨浠跺鐞嗙▼搴忎腑閫氳繃removeChild鍒犻櫎褰撳墠鑺傜偣鐨勬椂鍊欙紝涔熸樉绀哄唴瀛樻硠婕忋€?
绗洓绉嶏細鍦ㄥ垱寤篋OM瀵硅薄鏃舵彃鍏cript銆傝繖涓繕鏄涓€娆$湅鍒般€傚嵆鏄€氳繃createElement鍒涘缓DOM鍏冪礌鐨勬椂鍊欙紝鐩存帴鍦ㄥ瓧绗︿覆涓彃鍏ヤ簡js浠g爜锛歞ocument.createElement(“<div onclick=’foo();’>”)锛屼絾鏄繖绉嶆柟寮忓彧鍦↖E涓嬫湁鏁堛€傞€氳繃娴嬭瘯涓嬮潰鐨勭▼搴忥紝鍦―rip涓篃纭疄鏄剧ず鍐呭瓨娉勬紡浜?
var leakMemory=function(){ for(i = 0; i < 5000; i++){ var parentDiv = document.createElement("<div onClick='foo()'>"); } }
绗簲绉嶏細鎬绘槸鍏堝皢鏂板垱寤虹殑DOM瀵硅薄鎻掑叆鍒版枃妗e悗锛屽湪瀵瑰叾杩涜鍏朵粬鎿嶄綔銆傚浜庤繖鐐癸紝鎴戞兂璞′笉鍒板畠鏄浣曢€犳垚鍐呭瓨娉勬紡鐨勩€傝€屼笖锛屽畠璺熼〉闈紭鍖栫殑涓€浜涙柟寮忓彲鑳藉瓨鍦ㄥ啿绐併€傚湪鏌愪簺鎯呭喌涓嬶紝鍦ㄥ垱寤轰簡DOM鍏冪礌涔嬪悗锛屽厛澶勭悊DOM鐨勬搷浣滐紝鏈€鍚庢墠鎻掑叆鍒版枃妗d腑锛岃繖鏍峰彲浠ラ伩鍏嶅敖鍙兘鐨勭敱浜巖eflow褰卞搷鎬ц兘鐨勬儏鍐点€傝繖鍙兘灏遍渶瑕佷竴涓潈琛′簡鍚э紝鍥犲湴鍒跺疁~
鎬荤粨锛?/strong>
涓婇潰鏄湰浜洪€氳繃浣跨敤Drip宸ュ叿娴嬭瘯鐨勭粨鏋滐紝浣嗘槸鐢变簬鍦╯IEVE鍜孞S Leaps Detector涓嬫祴璇曢兘娌″彂鐜板唴瀛樻硠婕忕殑鎯呭喌锛屾墍浠ョ籂缁撶殑寰堛€傜粡杩囪繖涓€鐣姌鑵撅紝涔熶笉鏋夎嚜宸变竴鐣€掕吘鍊掕吘鍚э紝鍦ㄤ互鍚庣殑缂栧啓浠g爜涓紝鍙互鎴栧鎴栧皯鐨勫幓閬垮厤杩欎簺涓嶅繀瑕佺殑鍙兘閫犳垚鍐呭瓨娉勬紡鐨勬儏鍐靛嚭鐜般€?
鍚屾椂锛屽鏋滄湁璇撮敊鐨勫湴鏂癸紝娆㈣繋鎸囨锛屽叡鍚屽涔爚~
鏇村鍙傝€冿細銆?a href="http://www.bgscript.com/archives/410">濡備綍闃叉鍔ㄦ€佸姞杞絁avaScript寮曡捣鐨勫唴瀛樻硠婕忛棶棰?/a>銆嬶紝銆?a href="http://hi.baidu.com/_1_1_1_1/blog/item/7ed97338b0c97c2097ddd818.html">javascript 鍐呭瓨绠$悊 閬垮厤鍐呭瓨娉勬紡銆嬶紝銆?a href="http://blog.htmlor.com/2006/07/19/ie_jscript_leaks/">鍏充簬ie涓璲script鐨勫唴瀛樻硠婕?/a>銆嬶紝銆?a href="http://whcwc.javaeye.com/blog/607748">javascript鍨冨溇鍥炴敹鍜孖E鍐呭瓨娉勯湶銆嬶紝銆?a href="http://blog.csdn.net/Lunatic_Sun/archive/2008/03/20/2198812.aspx">闃叉Javascript閫犳垚IE鍐呭瓨娉勬紡鐨勮嫢骞插師鍒?/a>銆嬶紝銆?a href="http://ctiam.cn/lab/js/JScriptMemoryLeaks.html">JScript鍐呭瓨娉勬紡/ie鍐呭瓨娉勬紡銆嬶紝銆?a href="http://www.blogjava.net/tim-wu/archive/2006/05/29/48729.html">鍏充簬Javascript鐨勫唴瀛樻硠婕忛棶棰樼殑鏁寸悊绋?/a>銆嬶紝銆?a href="http://javascript.crockford.com/memory/leak.html">JScript Memory Leaks銆嬶紝銆?a href="http://msdn.microsoft.com/en-us/library/bb250448.aspx">Understanding and Solving Internet Explorer Leak Patterns銆嬶紝銆?a href="http://www.cnblogs.com/birdshome/archive/2006/05/28/ie_memoryleak.html">鐞嗚В骞惰В鍐矷E鐨勫唴瀛樻硠婕忔柟寮廩缈昏瘧]銆?/p>