`free` operator

Shawn Steele Shawn.Steele at microsoft.com
Thu Oct 25 17:35:15 PDT 2012

I sort of have a fundamental problem with the solution.  Eg:  If it were actually unused, it'd be GC'd.  Since it isn't GC'd, something must be holding a reference to it.  So if you force it to be gone, or clear all the properties, or whatever, seems to me that then you'd just start throwing random errors in the code that tried to use the "freed" object?  That might be even harder to track down.


-----Original Message-----
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of John J Barton
Sent: Thursday, October 25, 2012 5:21 PM
To: Isaac Schlueter
Cc: es-discuss
Subject: Re: `free` operator

On Thu, Oct 25, 2012 at 4:16 PM, Isaac Schlueter <i at izs.me> wrote:
> It'd be really nice if JS had a way to explicitly delete an object.

I guess you mean ... a way to set all the refs to a object to undefined.

> What do you folks think about a "free" operator (or something like it) 
> that would actually do what `delete` looks like it does?
> var obj = {}
> var foo = { ref: obj }

I assume that in your real life, you don't know 'foo' but somehow you know that foo.ref is never used?

Debuggers *should* be able to tell you all the places obj is used; I guess it's not very hard.

> var obj2 = obj
> free obj  // obviously this syntax probably won't work, since 'free'

Since you know "obj" can you set it to be a getter that returns undefined?

> is not a reserved word already
> assert(obj === undefined)
> assert(foo.ref === undefined)
> assert(obj2 === undefined)
> So, any references to the freed object would be set to undefined, and 
> presumably at some point, the GC will harvest it.
> Yes, yes, I know, the proper way to fix a memory leak in JS is to 
> properly manage references, but sometimes that means rewriting this 
> complicated app, and it's leaking memory now, in production.

Deleting all of the properties of obj would solve the problem as well I assume.

es-discuss mailing list
es-discuss at mozilla.org

More information about the es-discuss mailing list