1

Let's say I created an big object in a scope. It is referred to by child scopes, but all of them exits gracefully. And no closure referring to it at all. After this scope exits, I suppose the last reference to the object is deleted, right? Is the memory consumed by the object freed right away, or is it only marked as free and has to wait for the garbage collector to delete it?

syockit
  • 5,747
  • 1
  • 24
  • 33

1 Answers1

4

Garbage collection depends on the Javascript implementation. Theoretically, objects should be deleted after all references to them are gone. If you define an object in scope and nothing outside the scope references it (for example, adding it to an array outside the scope would be an outside reference), it should be collected sometime after you leave the scope.

This is a more detailed explanation.

Community
  • 1
  • 1
lunixbochs
  • 21,757
  • 2
  • 39
  • 47
  • So I take it to mean there's no guarantee the memory will be freed immediately after no more references are there? Is the object put on heap even without outside reference? – syockit Mar 07 '11 at 13:43
  • 1
    No, there's no such guarantee. (Unless you use a platform-dependent way to force a GC immediately after dropping the last reference: e.g. Mozilla's `Components.utils.forceGC()`) – CAFxX Mar 08 '11 at 06:44