472,791 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Deleting an anonymous function

What is the best practice for removing anonymous functions?

Something like
(function() { doSomething(); arguments.callee = null; })();

seems to work (at least it triggers no errors or exceptions on FF, but
is this really a working solution?

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur fr den alpinen Raum
Aug 1 '08 #1
7 2789
On Aug 1, 6:19*pm, Gregor Kofler <use...@gregorkofler.atwrote:
What is the best practice for removing anonymous functions?

Something like

(function() { doSomething(); arguments.callee = null; })();

seems to work (at least it triggers no errors or exceptions on FF, but
is this really a working solution?
You don't need to set arguments.callee to null. It is fine without
that line.
Aug 1 '08 #2
On Aug 2, 8:19*am, Gregor Kofler <use...@gregorkofler.atwrote:
What is the best practice for removing anonymous functions?
Leave them for the garbage collector. Do you attempt to explicitly
remove named functions?

It seems to me that:

var foo = function(){ /*foo body */ };
foo();
foo = null;

results in the function formerly referred to as foo becoming anonymous
and, if there are no other references to it, it becomes available for
garbage collection.

It is, more or less, equivalent to:

(function(){ /*foo body */ })();
provided foo isn't called from elsewhere before being set to null.

Something like

(function() { doSomething(); arguments.callee = null; })();

seems to work (at least it triggers no errors or exceptions on FF, but
is this really a working solution?
It depends on what you interpret from the phrase "seems to work". The
arguments object belongs on to the execution context, it is given a
'callee' property that refers to the anonymous Function so that it can
be recursive. It has the property DontEnum, but is not specified as
being ReadOnly so you can set it to anything you like without causing
an error (in a browser conforming to ECMA-262, section 10.1.8).

Setting arguments.callee as a reference to some arbitrary object
doesn't seem to do anything useful and should have zero effect on when
the function is made available for garbage collection.
--
Rob
Aug 2 '08 #3
David Mark meinte:
On Aug 1, 6:19 pm, Gregor Kofler <use...@gregorkofler.atwrote:
>What is the best practice for removing anonymous functions?

Something like

(function() { doSomething(); arguments.callee = null; })();

seems to work (at least it triggers no errors or exceptions on FF, but
is this really a working solution?

You don't need to set arguments.callee to null. It is fine without
that line.
To clarify that: An anonymous function gets removed by GC even if it's
content is "more complicated" including closures?

Something like:

(function() {
var counter = 0, id;
id = window.setInterval(function() {
document.write(counter++);
if(counter 100) {
window.clearInterval(id);
}
}, 1000)
})();

Well, perhaps I'm just suffering some memory-leak-paranoia...

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur fr den alpinen Raum
Aug 2 '08 #4
Thomas 'PointedEars' Lahn meinte:
>Well, perhaps I'm just suffering some memory-leak-paranoia...

I think your concern is certainly based on reasonable grounds.
I've just tried this on FF w/ Firebug:

for(var k = 100; --k;) {
(function() {
var stack = [], id, counter = 0;
id = window.setInterval(function() {
var s = [];
for(var j = 1000; j--;) {
s.push("xyz", "abc", "def");
}

stack.push(s);
console.log(counter);

if(++counter 100) {
window.clearInterval(id);
}
}, 100)
})();
}

The memory usage of FF goes up from approx. 76MB to around 290MB, then
(after clearing the console) to 77MB, a reload pushed it again to 290MB
before returning to (hooray!) 76MB.

It's of course a bit pedestrian, but the GC seems to collect all the
unused stuff properly.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Aug 2 '08 #5
Richard Cornford meinte:

[something I'll try to grasp later]
>>Well, perhaps I'm just suffering some
memory-leak-paranoia...

I think your concern is certainly based on reasonable
grounds.

Given the increase in AJAX projects where the intention is not to
navigate way form a 'page' for long periods of time and the increased
use of (less then optimally implemented) functional programming
structures (particularly in 'popular' libraries) it does seem reasonable
to gauge the extent to which this is an issue.
I idea is some "flash-once" highlighting of elements on the page; a
setTimeout/setInterval that does some color or opacity manipulation
packaged in an anonymous function - fire and forget. However, in this
very application one could accumulate quite a few of those highlighters.
Executing these scripts in one page and then navigating to an
"about:blank" homepage and recording the defences in memory use between
the two pages loaded states, and repeating a few times, the results were:-

For test A the difference between "about:blank" loaded and the post-test
code execution of the test page were; ~76 Megabytes more for the test
script.

Test B; ~145 Megabytes

Test C; ~128 Megabytes

Conclusion: In the environment tested (JScript) the normal process does
not raise an issue. If, however, the - arguments - object is explicitly
preserved then the memory consumption does increase significantly, and
given that explicitly clearing the - arguments.callee - in test C did
decrease the extent of that memory increase it would be reasonable to
conclude that some of that memory increase (~17 Megabytes) is accounted
for by now non-garbage collectable outer function objects.
Thanks for testing - very interesting indeed.
If nobody else gets there first I will try out similar tests on other
browsers soonish.
Greatly appreciated.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Aug 2 '08 #6
Gregor Kofler wrote:
Thomas 'PointedEars' Lahn meinte:
>>Well, perhaps I'm just suffering some
memory-leak-paranoia...

I think your concern is certainly based on reasonable
grounds.

I've just tried this on FF w/ Firebug:

for(var k = 100; --k;) { (function() {
var stack = [], id, counter = 0;
id = window.setInterval(function() {
var s = [];
for(var j = 1000; j--;) {
s.push("xyz", "abc", "def");
}

stack.push(s);
console.log(counter);

if(++counter 100) {
window.clearInterval(id);
}
}, 100)
})();
}
The memory usage of FF goes up from approx. 76MB to around
290MB, then (after clearing the console) to 77MB, a reload
pushed it again to 290MB before returning to (hooray!) 76MB.

It's of course a bit pedestrian, but the GC seems to collect
all the unused stuff properly.
This is a test for a memory leak, and not finding one is good. But I
don't think anyone was expecting to find a memory leak here anyway.
Circular chains of references between pure javascript objects have never
been a garbage collection issue once there are no external references to
any of the objects involved.

You have shown ~2 Megabytes per loop iteration of memory consumption,
and those of us who used 1980's computers, where having 2 Megabytes of
memory would have been unusual, may find that a little shocking. For an
in-page AJAX web application the important question may not only be the
overall garbage collection effectives but also the memory footprint of
the running application, so if that per iteration memory consumption
could be reduced by freeing more objects sooner that may be significant.
You have shown that the outer function gets garbage collected
eventually, but not that it can be garbage collected as soon as its
execution context is finished.

Richard.

Aug 2 '08 #7
Gregor Kofler wrote:
Richard Cornford meinte:
<snip>
>>>Well, perhaps I'm just suffering some
memory-leak-paranoia...

I think your concern is certainly based on reasonable
grounds.
<snip>
>... it does seem reasonable to gauge the extent to which this is an
issue.
<snip>
>Conclusion: In the environment tested (JScript) the normal
process does not raise an issue. ... .

Thanks for testing - very interesting indeed.
>If nobody else gets there first I will try out similar tests
on other browsers soonish.
<snip>

Looking at this on Firefox and Opera (various recent versions) produced
pretty much the same pattern as IE. So there is no need to worry about
closures needlessly preserving references to outer function objects
through - arguments/arguments.callee - in those environments either.

Windows Safari was more of a problem as the test code used for Opera and
Firefox killed its ability to execute javascript (strangely, as it was
quite happy to carry on displaying HTML pages and navigate, etc. it just
would not execute any more javascript of any sort until shut down and
re-started). It seems that the Safari's issue related directly to the
number of closures being formed (the number of iterations in the loops).
Unfortunately reducing the length of the loops to the point where Safari
could survive the test code resulted in the changes in memory use
measured for the tests being too small to be conclusive.

Richard.

Aug 30 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
1
by: Robert Batt | last post by:
Hello, I have a problem deleting from a datagrid bound to a dataset. then datagrid is bound as follows MyDatagrid.DataSource = Mydataset.Tables(strtable this works fine for adding and...
19
by: laredotornado | last post by:
Hello, Using PHP 4, what are the shortest amount of lines I can write to delete all the files in a given directory? Thanks for your help, - Dave
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.