la***@o2.pl wrote:
Hi
I'm creating now a site which is using heavly AJAX and what comes with
it, many Javascript functions are used. However many of these functions
are used only once (mainly to load new content by sending request to
server and by using DOM editing site content based on the response). So
keeping these functions in memory, while new content with new functions
is loaded, would be just wasting of memory.
And here goes my question - is there any way to delete Javascript
functions or free memory used by them? I've tried with "delete"
operator but it doesn't work with functions. I thinked also about
setting the function name to "null", but I'm not sure if Javascript
will automaticlly free memory.
You can do it by changing the reference to any value, it might as well
be null unless you feel 'undefined' is better. e.g.
function foo () { ... }
Creates a property of the global object with a value that is a
reference to the function object 'foo'.
window.foo = 'blah';
Replaces the value of window.foo with the primitive string 'blah'.
There are no more references to the function object foo, so it can be
collected.
Removing all references to an object (or leaving only circular
references between objects) should make it available, however you don't
have any control over when it runs[1] and it may not run until the page
is unloaded.
There is a useul thread here (take note of Richard's stuff only):
<URL:
http://groups.google.com/group/comp....444fd554f6d112
>
1. Some browsers allow you to expressly call garbage collection, but
not many (only IE that I know of).
--
Rob