{"id":9658,"date":"2008-12-14T18:18:37","date_gmt":"2008-12-14T10:18:37","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=9658"},"modified":"2008-12-14T18:18:37","modified_gmt":"2008-12-14T10:18:37","slug":"%e6%b5%8f%e8%a7%88%e5%99%a8%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e6%b3%84%e9%9c%b2","status":"publish","type":"post","link":"http:\/\/www.fuhaijun.com\/?p=9658","title":{"rendered":"\u6d4f\u89c8\u5668\u4e2d\u7684\u5185\u5b58\u6cc4\u9732"},"content":{"rendered":"<div id=\"Published By Juziyue-[4]1_2008C1D6313C47968A9EDBE3224597C1_820F683A1D4D40E08787A20EB07C27DA\">\n<p>\u4ec0\u4e48\u662f\u5185\u5b58\u6cc4\u9732&nbsp;<\/p>\n<p>\u5185\u5b58\u6cc4\u9732\u662f\u6307\u4e00\u5757\u88ab\u5206\u914d\u7684\u5185\u5b58\u65e2\u4e0d\u80fd\u4f7f\u7528\uff0c\u53c8\u4e0d\u80fd\u56de\u6536\uff0c\u76f4\u5230\u6d4f\u89c8\u5668\u8fdb\u7a0b\u7ed3\u675f\u3002\u5728C++\u4e2d\uff0c\u56e0\u4e3a\u662f\u624b\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u5185\u5b58\u6cc4\u9732\u662f\u7ecf\u5e38\u51fa\u73b0\u7684\u4e8b\u60c5\u3002\u800c\u73b0\u5728\u6d41\u884c\u7684C#\u548cJava\u7b49\u8bed\u8a00\u91c7\u7528\u4e86\u81ea\u52a8\u5783\u573e\u56de\u6536\u65b9\u6cd5\u7ba1\u7406\u5185\u5b58\uff0c\u6b63\u5e38\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\u51e0\u4e4e\u4e0d\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u9732\u3002\u6d4f\u89c8\u5668\u4e2d\u4e5f\u662f\u91c7\u7528\u81ea\u52a8\u5783\u573e\u56de\u6536\u65b9\u6cd5\u7ba1\u7406\u5185\u5b58\uff0c\u4f46\u7531\u4e8e\u6d4f\u89c8\u5668\u5783\u573e\u56de\u6536\u65b9\u6cd5\u6709bug\uff0c\u4f1a\u4ea7\u751f\u5185\u5b58\u6cc4\u9732\u3002<\/p>\n<p><\/p>\n<p>\u5185\u5b58\u6cc4\u9732Quick View&nbsp;<\/p>\n<p>\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u4e2d\u5b58\u5728\u5404\u79cd\u5185\u5b58\u6cc4\u9732\u65b9\u5f0f\uff0c\u76ee\u524d\u53d1\u73b0\u7684\u4e3b\u8981\u662f\u8fd9\u6837\u51e0\u79cd\uff1a<\/p>\n<p><\/p>\n<p>1. &nbsp; &nbsp; &nbsp; \u5faa\u73af\u5f15\u7528&nbsp;<\/p>\n<p>\u5df2\u7ecf\u786e\u8ba4\u5b58\u5728\u6cc4\u6f0f\u7684\u6d4f\u89c8\u5668\uff1aIE6.0 FF2.0<\/p>\n<p><\/p>\n<p>\u542b\u6709DOM\u5bf9\u8c61\u7684\u5faa\u73af\u5f15\u7528\u5c06\u5bfc\u81f4\u5927\u90e8\u5206\u5f53\u524d\u4e3b\u6d41\u6d4f\u89c8\u5668\u5185\u5b58\u6cc4\u9732 \u8fd9\u91cc\u6709\u4e24\u4e2a\u7b80\u5355\u7684\u6982\u5ff5<\/p>\n<p><\/p>\n<p>\u5f15\u7528\uff1aa.\u5c5e\u6027=b\uff0ca\u5c31\u5f15\u7528\u4e86b<\/p>\n<p><\/p>\n<p>\u5faa\u73af\u5f15\u7528\uff1a\u7b80\u5355\u6765\u8bf4\u5047\u5982a\u5f15\u7528\u4e86b,b\u53c8\u5f15\u7528\u4e86a,a\u548cb\u5c31\u6784\u6210\u4e86\u5faa\u73af\u5f15\u7528\u3002<\/p>\n<p><\/p>\n<p>a\u548cb\u5faa\u73af\u5f15\u7528\uff1a<\/p>\n<p><\/p>\n<p>var a=new Object;<\/p>\n<p>var b=new Object;<\/p>\n<p>a.r=b;<\/p>\n<p>b.r=a;<\/p>\n<p><\/p>\n<p>a\u5faa\u73af\u5f15\u7528\u81ea\u5df1\uff1a<\/p>\n<p><\/p>\n<p>var a=new Object;<\/p>\n<p>a.r=a;<\/p>\n<p><\/p>\n<p>\u5faa\u73af\u5f15\u7528\u5f88\u5e38\u89c1\u4e14\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u662f\u65e0\u5bb3\u7684\uff0c\u4f46\u5f53\u53c2\u4e0e\u5faa\u73af\u5f15\u7528\u7684\u5bf9\u8c61\u4e2d\u6709DOM\u5bf9\u8c61\u6216\u8005ActiveX\u5bf9\u8c61\u65f6\uff0c\u5faa\u73af\u5f15\u7528\u5c06\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u3002\u6211\u4eec\u628a\u4f8b\u5b50\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2anew Object\u66ff\u6362\u6210document.getElementById\u6216\u8005document.createElement\u5c31\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u9732\u4e86\u3002<\/p>\n<p>\u5c3d\u7ba1\u8fd9\u770b\u8d77\u6765\u975e\u5e38\u5bb9\u6613\u7406\u89e3\uff0c\u4f46\u662f\u56e0\u4e3a\u6709closure\u7684\u53c2\u4e0e\u800c\u4f7f\u4e8b\u60c5\u53d8\u5f97\u590d\u6742\uff0c\u6709\u4e9bclosure\u5bfc\u81f4\u7684\u5faa\u73af\u5f15\u7528\u5f88\u96be\u88ab\u5bdf\u89c9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u52a8\u6001\u7ed1\u5b9a\u4e8b\u4ef6\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; obj.onclick=function(){<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; \/\/Even if it&#8217;s a empty function<\/p>\n<p>&nbsp; &nbsp; }<\/p>\n<p>}<\/p>\n<p>\u8fd9\u4e2abindEvent\u6267\u884c\u65f6100%\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u9732\uff0cSomeone \u53ef\u80fd\u4f1a\u95ee\uff0c\u54ea\u91cc\u51fa\u73b0\u4e86\u5faa\u73af\u5f15\u7528? \u5173\u4e8eclosure\u548cscope chain\u53c2\u4e0e\u7684\u5faa\u73af\u5f15\u7528\u6bd4\u8f83\u590d\u6742\uff0c\u6b64\u5904\u6682\u4e0d\u6df1\u5165\u8ba8\u8bba\u3002\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u5224\u65ad\u65b9\u5f0f\uff1a\u51fd\u6570\u5c06\u95f4\u63a5\u5f15\u7528\u6240\u6709\u5b83\u80fd\u8bbf\u95ee\u7684\u5bf9\u8c61\u3002obj.onclick\u8fd9\u4e2a\u51fd\u6570\u4e2d \u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u7684\u53d8\u91cfobj \u6240\u4ee5\u4ed6\u5f15\u7528\u4e86obj,\u800cobj\u53c8\u5f15\u7528\u4e86\u5b83\uff0c\u56e0\u6b64\u8fd9\u4e2a\u4e8b\u4ef6\u7ed1\u5b9a\u5c06\u4f1a\u9020\u6210\u5185\u5b58\u6cc4\u9732\u3002\u5728IBM\u7684\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u4e862\u79cd\u65b9\u5f0f\u89e3\u51b3\u7c7b\u4f3c\u7684\u95ee\u9898\u4e00\u4e2a\u662fobj=null\uff0c\u53e6\u4e00\u4e2a\u662f\u628aonclick\u7684\u51fd\u6570\u5199\u5728bindEvent\u5916\uff0c\u91cd\u590d\u4eba\u5bb6\u7684\u6211\u5c31\u4e0d\u8bf4\u4e86\u3002\u7b80\u5355\u8d34\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; obj.onclick=onclickHandler;<\/p>\n<p>}<\/p>\n<p>function onclickHandler(){<\/p>\n<p>&nbsp; &nbsp; \/\/do something<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; obj.onclick=function(){<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; \/\/Even if it&#8217;s a empty function<\/p>\n<p>&nbsp; &nbsp; }<\/p>\n<p>&nbsp; &nbsp; obj=null;<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u90fd\u6253\u65ad\u4e86\u5faa\u73af\u5f15\u7528\uff0c\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u662f\u4f3c\u4e4e\u5bf9\u4ee3\u7801\u8868\u8fbe\u80fd\u529b\u9020\u6210\u4e86\u4e00\u5b9a\u7834\u574f\uff0c\u5047\u8bbe\u6709\u8fd9\u4e48\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; var var0=&#8221;OOXX&#8221;;\/\/Here is a variable<\/p>\n<p>&nbsp; &nbsp; obj.onclick=function(){<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; alert(var0);\/\/I want to visit var2 here!<\/p>\n<p>&nbsp; &nbsp; }<\/p>\n<p>&nbsp; &nbsp; return obj;\/\/bindEvent must return obj!<\/p>\n<p>}<\/p>\n<p>\u597d\u4e86 \u8fd9\u4e0b\u4e24\u79cd\u529e\u6cd5\u90fd\u4e0d\u884c\u4e86\uff0c\u5047\u5982\u6211\u628a\u51fd\u6570\u5199\u5916\u9762\u53bb\uff0cvar0\u80af\u5b9a\u8bbf\u95ee\u4e0d\u4e86\uff0c\u5047\u5982\u6211\u628aobj\u5f04\u6210null\uff0c\u8fd8\u600e\u4e48return\u5b83\u5462\uff1f\u8fd9\u5e76\u4e0d\u662f\u7a7a\u60f3\u7684\u9700\u8981\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7528JS\u5b9a\u5236DOM\u63a7\u4ef6\u7684\u7b80\u5355\u62bd\u8c61\uff1a\u521b\u5efaDOM\u5143\u7d20\u3001\u8bbe\u7f6e\u79c1\u6709\u5c5e\u6027\u3001\u7ed1\u5b9a\u4e8b\u4ef6\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5fc5\u987bupdate\u4e00\u4e0b\u4e24\u4e2a\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u65b9\u6cd51\uff0c\u4e3a\u4e86\u8ba9\u51fd\u6570\u80fd\u8bbf\u95ee\u67d0\u4e9b\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2aBuilder\u51fd\u6570\u6765\u8ba2\u5236onclick\u7684\u5916\u90e8\u95ed\u5305\uff1a<\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; var var0=&#8221;OOXX&#8221;;\/\/Here is a variable<\/p>\n<p>&nbsp; &nbsp; obj.onclick= onclickBuilder(var0);\/\/\u60f3\u8bbf\u95ee\u8c01\u5c31\u628a\u8c01\u4f20\u8fdb\u53bb\uff01\uff01<\/p>\n<p>&nbsp; &nbsp; return obj;\/\/bindEvent must return obj!<\/p>\n<p>}<\/p>\n<p>function onclickBuilder(var0)\/\/\u8fd9\u91cc\u8ddf\u4e0a\u9762\u5bf9\u5e94\u4e0a\u5c31\u884c\u4e86 \u6700\u597d\u53c2\u6570\u540d\u5b57\u4e5f\u5bf9\u5e94\u4e0a<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; return function(){<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; alert(var0);<\/p>\n<p>&nbsp; &nbsp; }<\/p>\n<p>}<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p>\u7b2c\u4e8c\u4e2a\u529e\u6cd5\uff0c\u8fd9\u4e2a\u6765\u81ea51js\u7684chpn\u540c\u5b66\uff0c\u8ba9obj=null\u5728return \u4e4b\u540e\u6267\u884c\uff01\uff01<\/p>\n<p><\/p>\n<p>function bindEvent()<\/p>\n<p>{<\/p>\n<p>&nbsp; &nbsp; try{<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; var obj=document.createElement(&#8220;XXX&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; var var0=&#8221;OOXX&#8221;;\/\/Here is a variable<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; obj.onclick=function(){<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(var0);\/\/I want to visit var2 here!<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return obj;\/\/bindEvent must return obj!<\/p>\n<p>&nbsp; &nbsp; } finally {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; obj=null;<\/p>\n<p>&nbsp; &nbsp; }<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><\/p>\n<p>2. &nbsp; &nbsp; &nbsp; \u67d0\u4e9bDOM\u64cd\u4f5c&nbsp;<\/p>\n<p>\u8fd9\u662fIE\u7cfb\u5217\u7684\u7279\u6709\u95ee\u9898 \u7b80\u5355\u7684\u6765\u8bf4\u5c31\u662f\u5728\u5411\u4e0d\u5728DOM\u6811\u4e0a\u7684DOM\u5143\u7d20appendChild\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5185\u5b58\u6cc4\u9732\uff08\u53ea\u662f\u53ef\u80fd\uff0c\u5177\u4f53\u539f\u56e0\u4e0d\u660e\uff0c\u4f3c\u4e4e\u5341\u5206\u590d\u6742\uff0c\u4e0b\u9762\u4f8b\u5b50\u4e2d\u53bb\u6389onClick\u4e5f\u53ef\u4ee5\u907f\u514d\u6cc4\u9732\uff09\u3002\u6240\u4ee5appendChild\u7684\u987a\u5e8f\u53ef\u80fd\u5f71\u54cd\u5185\u5b58\u6cc4\u9732\uff0c\u6765\u81ea\u5fae\u8f6f\u7684\u4f8b\u5b50\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp; &nbsp; &lt;head&gt;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &lt;script language=&#8221;JScript&#8221;&gt;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; function LeakMemory()<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hostElement = document.getElementById(&#8220;hostElement&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Do it a lot, look at Task Manager for memory response<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(i = 0; i &lt; 5000; i++)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var parentDiv =<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.createElement(&#8220;&lt;div onClick=&#8217;foo()&#8217;&gt;&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var childDiv =<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.createElement(&#8220;&lt;div onClick=&#8217;foo()&#8217;&gt;&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ This will leak a temporary object<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentDiv.appendChild(childDiv);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hostElement.appendChild(parentDiv);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hostElement.removeChild(parentDiv);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentDiv.removeChild(childDiv);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentDiv = null;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; childDiv = null;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hostElement = null;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; }<\/p>\n<p><\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; function CleanMemory()<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hostElement = document.getElementById(&#8220;hostElement&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Do it a lot, look at Task Manager for memory response&nbsp;<\/p>\n<p><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5185\u5b58\u6cc4\u9732&nbsp; \u5185\u5b58\u6cc4\u9732\u662f\u6307\u4e00\u5757\u88ab\u5206\u914d\u7684\u5185\u5b58\u65e2\u4e0d\u80fd\u4f7f\u7528\uff0c\u53c8\u4e0d\u80fd\u56de\u6536\uff0c\u76f4\u5230\u6d4f\u89c8\u5668\u8fdb\u7a0b\u7ed3\u675f\u3002\u5728C++\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-9658","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/9658"}],"collection":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9658"}],"version-history":[{"count":0,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/9658\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9658"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}