Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JScript memory management (garbage collection?)

21 views
Skip to first unread message

Matt

unread,
Oct 6, 2003, 11:21:52 AM10/6/03
to
I am writing a client-side dhtml framework in jscript for IE5.5. As
new objects are created, they take up memory (of course.) I have
designed it so that whenever an object is unloaded a free() method is
called to call a delete on all of its properties. I then set the
object itself to null and call the undocumented CollectGarbage() in
hopes of freeing up memory. Well, CollectGarbage() does nothing. After
a short amount of time I have used all my memory. Until I either
minimize the window or navigate to a new page, the memory will not be
released. Since the framework is basically a static, single-page UI,
navigating to a new page and minimizing are not options. Am I using
CollectGarbage() wrong? From what I understand, setting an object to
null marks it for deletion by the garbage collector (which I have
never seen run on its own I should add,) but the object is obviously
not being freed.

Steve van Dongen [MSFT]

unread,
Oct 9, 2003, 10:14:33 PM10/9/03
to
On 6 Oct 2003 08:21:52 -0700, ma...@senternet.com (Matt) wrote:

>I am writing a client-side dhtml framework in jscript for IE5.5. As
>new objects are created, they take up memory (of course.) I have
>designed it so that whenever an object is unloaded a free() method is
>called to call a delete on all of its properties. I then set the
>object itself to null and call the undocumented CollectGarbage() in
>hopes of freeing up memory. Well, CollectGarbage() does nothing. After
>a short amount of time I have used all my memory. Until I either
>minimize the window or navigate to a new page, the memory will not be
>released. Since the framework is basically a static, single-page UI,
>navigating to a new page and minimizing are not options. Am I using
>CollectGarbage() wrong?

Yes, calling CollectGarbage() is wrong. It's undocumented for a
reason. It won't solve any of your problems.

>From what I understand, setting an object to
>null marks it for deletion by the garbage collector
> (which I have never seen run on its own I should add,)
>but the object is obviously not being freed.

That's not technically correct. Memory can only be garbage collected
when it nothing references it.

Example 1:
var a = { foo: 'bar' }
a = null;
// At this point the memory used for the object can be
// garbage collected

Example 2:
var a = { foo: 'bar' }
var b = a;
a = null;
// The object cannot be collected even though you set a=null.
// b still references it
b = 5;
// b no longer references the object. The memory can be
// collected now.

Memory will not necessarily be garbage collected immediately after it
no longer has any references pointing to it; the garbage collector
runs periodically.

Closures are a common source of memory leaks...

Whenever you use nested functions, a closure is created, giving the
enclosed function access to all of the outer function's arguments and
local variables. The problem with closures is how relatively easy it
is to create circular references when you don't realize what is really
going on. The JScript garbage collector is capable of breaking
circular references between any number of JScript objects, but it
cannot break a circular reference where the chain includes an external
object like an element in IE or an ActiveX object.

So, the point I'm trying to make here is basically, don't mix nested
functions and DHTML because you might end up with a circular reference
that cannot be broken and, therefore, a memory leak.

This is an example of a function that leaks memory...
function omicron()
{
var x = document.createElement("div");
x.foo = epsilon;
function epsilon()
{
...
}
}

x refers to a new div element in the DOM. The div has a property foo
that refers to the JScript function, epsilon. epsilon is a nested
function, therefore, it has access to the outer functions' arguments
and local variables, which in this case is the x variable.
To be clear, the circular reference is x --> div --> epsilon --> x

In this example, in theory you should be able to break the circular
reference by setting x=null at the end of omicron() *AND* setting the
div's foo property to null before removing the div from the DOM.

So there you go... If you need more help, I suggest you post a URL
and/or some code we can look at along with a description of what you
do to cause the memory leak.

Regards,
Steve
--
Please post questions to the newsgroup; everyone benefits.
This posting is provided "AS IS" with no warranties, and confers no rights.
Sample code subject to http://www.microsoft.com/info/cpyright.htm

Anand

unread,
Oct 10, 2003, 11:00:35 AM10/10/03
to
Its a real good topic which I was looking for long.
I also tried what 'Matt' said but no use.
But yes, Stev, as you said I do have nested functions and the work is like
what Mat said. in my case I open lots of iframes to load and place
applications.
Now my question even when the whole iframe is closed, still memory
stays in higher level. opening another iframe now increments this and memory
reaches 45 to 50 MB.
Any clue here ??
Regards
anand

"Steve van Dongen [MSFT]" <ste...@online.microsoft.com> wrote in message
news:bf3covselun1940ch...@4ax.com...

0 new messages