Explict Memory Managment

Brendan Eich brendan at mozilla.com
Fri May 22 13:37:17 PDT 2009

On May 22, 2009, at 1:27 PM, David Semeria wrote:

> On Fri, 2009-05-22 at 11:46 -0700, Brendan Eich wrote:
>> As Ash points out, there's no memory-safe way to implement obj.kill()
>> short of a full GC.
>> There is no way JS will lose memory safety, ever!
> Why can't the deleted object still exist, with its value set to  
> 'null'?

First, null is not a "value" in the sense of object value. An object  
takes up arbitrary space associating property names with values.  
You're possibly confusing reference and referent again.

But say you safely "nulled" all the memory associated with the object,  
making it equivalent to ({}) but preserving its identity. Then you  
haven't necessarily saved much memory (it could be small with only  
primitive values) and you still will booby-trap other code that saved  
aliasing references.

If you want kill that works on user-defined objects, you can write it  

function kill(obj) {
     for (var i in obj)
         delete obj[i];

But you'd do better to set obj = null to kill the reference, instead  
of wasting VM cycles deleting enumerable properties but leaving the  
empty object alive.

>> If you are using too much memory, reifying large
>> external data sets in JS, don't do that. There's usually a way to
>> avoid taking the all-in-memory hit. SAX-style parsing rather than  
>> all-
>> or-nothing blocking parse calls, etc.
> This goes back to my initial point regarding complex web apps. There  
> are
> many intances where it makes sense to store content in js objects and
> then flow it into the DOM using a given template. If the user changes
> the template then the information is simply reflowed.

What's the problem? GC runs sooner or later. If you see a GC bug,  
please report it.

Yes, GC uses more memory on average than if you gave explicit advice  
that was reliable. But the advice can't be trusted without walking the  
object graph.

> I intentionally didn't go into specifics, but I can provide some real
> world examples if you think they would be helpful.



More information about the es-discuss mailing list