Re: How to free memory in Javascript?

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Ian Boyd" <ian.msnews010@xxxxxxxxxxxx> wrote in message
news:eNzdSC8%23IHA.3392@xxxxxxxxxxxxxxxxxxxxxxx
It does, relax, assuming you aren't making the above mentioned COM
object
in
circular reference error

You understand my concern though. i, like most people, assumed that
Javascript couldn't leak memory - because it's all automagically garbage
collected.

Then people discover that it is possible to leak memory (by doing this, or
that, or even that). So now i'm paranoid that i'm doing something wrong
somehow and i am leaking memory. My object oriented self is paranoid about
destroying objects i construct.

So with a crisis of confidence in JScript, and the fact that i'm not
destroying anything - who knows whats going on...


Lets be clear. JScript itself doesn't leak memory.

Example:-

function MyClass()
{
var child
this.AddChild = function(value)
{
child = value
}
}

function DoThis()
{
var x = new MyClass()

x.AddChild(x)
}

DoThis()


Clearly a circular reference is used here but this won't leak memory.

However as soon as something external to JScript that uses a reference
counted means to deterministically destroy objects is introduced then the
possibility of mem leaks appear.

That said any fear you might have that simply using such an object without
specifically destroying it (which you can't do anyway) is unfounded. When
accessed by JScript a garbage collected wrapper is created for these objects
and when the that wrapper is collected the reference it is holding is
released.

It seems you have an aversion to talk about Circular references and
closures. If you feel that they are irrelevant because you are certain you
aren't using them in any form, then you don't have a memory leak.

The danger of course is that you may be using them unwittingly, usually the
way to escape paranioa and superstition is via education. If you would like
to know more about how you can confirm your code doesn't include any
patterns that will leak then let me know, I'll not bang on about it unless
you're actually interested.


--
Anthony Jones - MVP ASP/ASP.NET



.