[thelist] JS memory leak detection?

liorean liorean at gmail.com
Mon Oct 24 06:04:26 CDT 2005


On 10/24/05, VOLKAN ÖZÇELİK <volkan.ozcelik at gmail.com> wrote:
>
> Please correct me if I'm wrong in any of the following.
>
> function myFunc(){
> var obj=document.getElementById("somenode");
> var innerFunction=function(){
> }
> }
>
> innerFunction closes over obj (hence the over the node with id
> "somenode").


As well as over the innerFunction variable itself, yes

function myFunc(){
> var obj=document.getElementById("somenode");
> var innerFunction=function(){
> }
> obj=null;
> }
>
> Since obj is marked for garbage collection by setting it to null;
> innerFunction is free on unload because what it closes over will be
> gc'ed.


Well, in this particular case, obj should be garbage collected in any case.
With or without obj being set to null, because you haven't created a
circular reference. If you had obj.onmouseover=innerFunction there, or some
similar code to create a circular reference, setting obj to null would break
the circular reference and thereby allow the objects to be garbage
collected.


function myFunc(){
> var obj=document.getElementById("somenode");
> var innerFunction=function(){
> var myInenrVar=document.getElementById("someothernode");
> }
> obj=null;
> }
>
> myInnerVar does not create any leak because it is deallocated as soon
> as innerFunction exits.
>

Yes.
--
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>


More information about the thelist mailing list