下面是一些代碼,我在谷歌的Chrome 19.0.1061.1運行(正式版本125213)開發:內存泄漏一個XMLHttpRequest和setInterval
<html>
<title>Memory Leak</title>
<script type="text/javascript">
(function(){
this.window.setInterval(function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '', false);
xhr.send();
}, 50);
}).call(this);
</script>
</html>
當我在Chrome檢查內存使用://任務,我可以看到「私人內存」無限增長(8GB內存配置)。 如果我的代碼示例更改上面類似的東西:
<html>
<title>Memory Leak</title>
<script type="text/javascript">
(function(){
var xhr = new XMLHttpRequest();
var timeout = this.window.setInterval(function() {
xhr.open('GET', '', false);
xhr.send();
}, 50);
}).call(this);
</script>
</html>
它現在確定。
我不明白。 爲什麼保持對setInterval函數的引用有幫助,而爲什麼定義只有一個xhr有幫助,因爲前面的聲明是在閉包中?它僅與v8有關嗎?
我將不勝感激您的見解。
保持對超時的引用並不能幫助清理泄漏。它重新使用XHR對象,防止泄漏。注意:如果您嘗試從服務器讀取響應,則會在50ms內被覆蓋,因此會中斷響應。 – 2012-03-21 14:06:58
- 爲什麼在範圍函數上使用調用? (function(context){console.log(context)/ * window * /})(this); (function)(context){console.log(context)/ * window * /})(window); (function(){console.log(this)/ * window * /})(); – elmuchacho 2012-03-21 14:44:49