"These memory leaks oftenoccur as a result of circular references between JavaScript objects andobjects within IE’s DOM (document object model)." GPDE Team Blog
明显的DOM对象与 JavaScript对象循环引用很好判断,难的是隐含的循环引用判断!
隐含的循环引用需要通过作用域链进行分析判断!
考虑以下代码(感谢可爱的winter ^_^):
var e=document.getElementBy??("XX");
e.οnclick=function(){}
scopechain分析:
scopechain.e = document.getElementBy??("XX");
function f(){}
function f(){}.[[scope]] = scopechain
e.onclick = f
很清晰,对不对?
"Javascript绑定事件时,只要DOM的事件里访问不了DOM自己的那个变量就行了" winter
"IE现在的Patch搞得有时候泄露有时候不泄露" winter
<button id="testx">No Memory Leak Event Bind</button>
<button id="testy">No Memory Leak Event Bind</button>
<script type="text/javascript">
var fooA = function(){
var f = function(){alert(this+'/n'+fooA);}
!function(){
var d = document.getElementById('testx');
d.onclick = f;
}()
}
fooA();
var fooB = function(){
this.onclick = function(){alert(this+'/n'+fooB);}
}
fooB.call(document.getElementById('testy'));
</script>