Re: How to free memory in Javascript?
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Thu, 7 Aug 2008 23:09:15 +0100
"Ian Boyd" <ian.msnews010@xxxxxxxxxxxx> wrote in message
news:%23NQ5bOJ%23IHA.4320@xxxxxxxxxxxxxxxxxxxxxxx
How do i free memory in Javascript?
I'm gonna answer these questions but if I were you I would ignore them and
skip to the bottom
Example 1:
var i;
i = 1;
How do i free i?
You can't. i is now an attribute in the global namespace and will only go
away when the script ends. In browser terms this will be when the user has
navigated to another page and the browser has decided to drop the page
(which for speedy back button purposes may not be for a while yet).
Example 2:
var i;
i = 1;
i = 2;
How do i free the value 1 stored in i before i assign 2?
The only memory allocated here is the space allocated for i. Changing it
value from 1 to 2 in this primitve way doesn't allocated or deallocate any
memory.
Example 3:
function DoStuff()
{
var i = 1;
}
How do i free i?
i is freed when the execution context object created when DoStuff was called
is garbage collected.
Example 4:
var button;
ExecuteButton = document.getElementById("buttonExecute");
How do i get rid of the reference to button?
Assuming you meant:-
var button = document.getElementById("buttonExecute");
then
button = null
However this still requires garbage collection, the JScript wrapper for a
COM object needs to be collected before it calls release on the aquired COM
object.
Example 5:
var saleDate;
saleDate = new Date();
How do i free the Date object?
saleDate = null;
Still requires a collection before the memory allocated by Date() can be
freed.
Example 6:
var saleDate;
saleDate = new Date();
saleDate = new Date();
How do i get rid of the first Date object?
Garbage collector will do it in its own sweet time.
Example 7:
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
How do i free the memory associated with the array?
myCars = null;
You know the drill.
Example 8:
var t = setTimeout("DoStuffTimer()", 1000);
How do i free the timer associated with t?
clearTimeout(t);
Example 9:
var t;
t = setTimeout("DoStuffTimer()", 1000);
t = setTimeout("DoStuffTimer()", 500);
How do i free the first timer?
var t;
t = setTimeout("DoStuffTimer()", 1000);
clearTimeout(t);
t = setTimeout("DoStuffTimer()", 500);
counted
i used to assume that javascript was a garbage collected, reference
language, and that as soon as a variable goes out of scope (and as suchall
references to it are released) the memory is freed by the garbagecollector.
But i cannot find anything to back up my feeling.
Whilst in some sense reference counting is a garbage collection pattern
which places the responsibility for clean up with the object to be
collected, generally the term 'garbage collected' is used as an opposite to
'reference counted'.
IOW the fact that a language is 'garbage collected' implies it doesn't do
reference counting and you cannot depend on allocated resources to be freed
as soon as all variables referencing them have gone out of scope.
VBScript is not garbage collected and therefore references to objects will
be released when a variable holding such a reference passes out of scope or
is assigned another value. Typically such object will free allocated
resources if there reference count hits 0. (There are exceptions such as
MSXML which internally uses a garbage collector).
JScript is a garbage collected language and therefore things do not get
freed straight away. A garbage collector will run at a time and/or in
circumstances that the JScript developers see fit.
JScript (not Javascript) has an undocumented (is document in the .NET
version) method: CollectGarbage() which forces the collector to run. I
don't recommend using it, it should not be necessary.
i've tried googling for such questions, but people are intent on talking
about memory leaks caused by circular references and closures. i'm not
interested in those, nor am i interested in memory that is freed when the
browser navigates away from a page.
In light of what you say below you should pay close attention to the people
are intent talking about circular references involving COM objects and
closures.
freed.
i have a page that will be running for weeks straight, if not months or
years - i want to know that a local variable in a function is being freed
once it goes out of scope. i was to know that everything is an object, and
once that object has no references it too is freed.
And i cannot find anything to indicate that memory i'm using is ever
It does, relax, assuming you aren't making the above mentioned COM object in
circular reference error (COM object would include the DHTML DOM objects)
then it will be fine.
--
Anthony Jones - MVP ASP/ASP.NET
.
- Follow-Ups:
- Re: How to free memory in Javascript?
- From: Ian Boyd
- Re: How to free memory in Javascript?
- References:
- How to free memory in Javascript?
- From: Ian Boyd
- How to free memory in Javascript?
- Prev by Date: Re: How to free memory in Javascript?
- Next by Date: Suppress error message pop-ups in HTA application
- Previous by thread: Re: How to free memory in Javascript?
- Next by thread: Re: How to free memory in Javascript?
- Index(es):