admin管理员组文章数量:1302333
Here's some code that I run on Google Chrome 19.0.1061.1 (Official Build 125213) dev:
<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>
When I inspect memory usage in chrome://tasks, I can see that "Private Memory" is growing up indefinitely (8GB RAM config). If I change the sample of code above to something like that:
<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>
It's now OK.
I don't get it. Why keeping a reference to the setInterval function helps and why defining only one xhr helps since previous declaration was in a closure? Is it related only to v8?
I would appreciate your insights on it.
Here's some code that I run on Google Chrome 19.0.1061.1 (Official Build 125213) dev:
<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>
When I inspect memory usage in chrome://tasks, I can see that "Private Memory" is growing up indefinitely (8GB RAM config). If I change the sample of code above to something like that:
<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>
It's now OK.
I don't get it. Why keeping a reference to the setInterval function helps and why defining only one xhr helps since previous declaration was in a closure? Is it related only to v8?
I would appreciate your insights on it.
Share Improve this question edited Mar 21, 2012 at 14:14 François Beaufort asked Mar 21, 2012 at 13:59 François BeaufortFrançois Beaufort 5,6593 gold badges30 silver badges41 bronze badges 2- 1 Keeping a reference to the timeout isn't helping the clean up the leak. It is re-using the XHR object that keeps it from leaking. NOTE: If you are trying to read a response from the server this will break since it will get overwritten in 50ms. – John Strickler Commented Mar 21, 2012 at 14:06
- -Why using call on the scope function? (function(context){console.log(context)/*window*/})(this); (function(context){console.log(context)/*window*/})(window); (function(){console.log(this)/*window*/})(); – elmuchacho Commented Mar 21, 2012 at 14:44
3 Answers
Reset to default 8In the first one, you're instantiating a new XMLHttpRequest object on each call to the iterator function. The request objects will stick around at least until the HTTP requests plete. Initiating 200 HTTP requests per second is going to clog the browser up something fierce, since it won't actually perform all the requests; there's a limit to how many concurrent connections it'll open.
How long does this http call take? if this takes longer then 50ms (which is a very shot amount of time) then the first case will be creating more and more pending requests while then second case you are reusing the same XMLHttpRequest which may have the effect of canceling the previous call.
In your first example, you are calling a new instance of XMLHttpRequest() with each interval. In the second one, you instantiate a copy one time and use it throughout the life of the code. That's why in the first example you run out of memory.
本文标签: javascriptMemory Leak with an XMLHttpRequest and setIntervalStack Overflow
版权声明:本文标题:javascript - Memory Leak with an XMLHttpRequest and setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741702865a2393394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论