纠结的IE浏览器内存泄漏的测试| Javascript is dancing

鍓嶅嚑澶╁湪缂栧啓浠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>

郑重声明:资讯 【纠结的IE浏览器内存泄漏的测试| Javascript is dancing】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——