`free` operator

John J Barton johnjbarton at johnjbarton.com
Thu Oct 25 17:20:34 PDT 2012


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.

jjb


More information about the es-discuss mailing list