admin管理员组文章数量:1336148
I'm trying to make sure some vars wrapped in a closure will be released for garbage collection when I'm done with them. I'm unsure weather setting them to undefined, or deleting them would be sufficient. Any thoughts?
// run once for each photo, could be hundreds
$("img.photo").each( function(){
// create the vars to put in the callback
var photo = $(this);
var tmp = new Image();
// set the callback, wrapping the vars in its scope
tmp.onload = (function(p,t){
return function(){
// do some stuff
// mark the vars for garbage collection
t.onload = ?
t = ?
p = ?
})(photo, tmp)
// set the source, which calls onload when loaded
tmp.src = photo.attr("src")
})
I'm trying to make sure some vars wrapped in a closure will be released for garbage collection when I'm done with them. I'm unsure weather setting them to undefined, or deleting them would be sufficient. Any thoughts?
// run once for each photo, could be hundreds
$("img.photo").each( function(){
// create the vars to put in the callback
var photo = $(this);
var tmp = new Image();
// set the callback, wrapping the vars in its scope
tmp.onload = (function(p,t){
return function(){
// do some stuff
// mark the vars for garbage collection
t.onload = ?
t = ?
p = ?
})(photo, tmp)
// set the source, which calls onload when loaded
tmp.src = photo.attr("src")
})
Share
Improve this question
asked Oct 26, 2011 at 16:14
greggreggreggreg
12.1k7 gold badges40 silver badges53 bronze badges
3 Answers
Reset to default 7look at this post for some more details on garbage collection.
Since you are sending an anonymous function to .each
the function and all within will be garbage collected. Except for one part:
// set the callback, wrapping the vars in its scope
tmp.onload = (function(p,t){
return function(){
// do some stuff
// mark the vars for garbage collection
t.onload = ?
t = ?
p = ?
})(photo, tmp)
The function (function(p,t){ ... })(photo, tmp)
will be collected since it is anonymous and no longer referenced. But the function it returns will be added to tmp.onload
which will persist.
If you want to make sure things are collected set variables, when you are done with them, to undefined or null. That way the garbage collector will be sure that there is no reference in scope, thus freeing them.
When your "onload" function exits, nothing will be referencing the closure itself, so the whole thing will be collected.
JavaScript takes care of the variables whose scope has left execution for those variable declared with var keyword.
For object declared use delete keyword to release memory.
x = new Object;
alert(x.value);
delete (x);
alert(x); //this wouldn't show
You may also find something at What is JavaScript garbage collection?.
本文标签: javascript make sure objects in closure are garbage collectedStack Overflow
版权声明:本文标题:javascript: make sure objects in closure are garbage collected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742395787a2466891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论