473,668 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting an anonymous function

What is the best practice for removing anonymous functions?

Something like
(function() { doSomething(); arguments.calle e = 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 für den alpinen Raum
Aug 1 '08 #1
7 2859
On Aug 1, 6:19*pm, Gregor Kofler <use...@gregork ofler.atwrote:
What is the best practice for removing anonymous functions?

Something like

(function() { doSomething(); arguments.calle e = 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.calle e to null. It is fine without
that line.
Aug 1 '08 #2
On Aug 2, 8:19*am, Gregor Kofler <use...@gregork ofler.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.calle e = 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.calle e 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...@gregork ofler.atwrote:
>What is the best practice for removing anonymous functions?

Something like

(function() { doSomething(); arguments.calle e = 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.calle e 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.setInter val(function() {
document.write( counter++);
if(counter 100) {
window.clearInt erval(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 für 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.setInter val(function() {
var s = [];
for(var j = 1000; j--;) {
s.push("xyz", "abc", "def");
}

stack.push(s);
console.log(cou nter);

if(++counter 100) {
window.clearInt erval(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:blan k" 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:blan k" 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.calle e - 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.setInter val(function() {
var s = [];
for(var j = 1000; j--;) {
s.push("xyz", "abc", "def");
}

stack.push(s);
console.log(cou nter);

if(++counter 100) {
window.clearInt erval(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.calle e - 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
14016
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
1741
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 updating, but when I try and delete something I often get a system.indexoutofrangeexception What is this about? there is nothing in the knowledgeabase about this Any ideas what is going wron
19
2284
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
2454
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 logic would help me accomplish this?
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.