>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
"Steve van Dongen [MSFT]" <ste...@online.microsoft.com> wrote in message
news:bf3covselun1940ch...@4ax.com...