-1

I have the following code:

var o = (function() {
    var o1 = {name: "o1"};
    var o2 = {name: "o2"};
    var o3 = {name: "o3"};

    o1.child = o2;
    o2.child = o3;
    o3.parent = o2;

    setTimeout(function() {
        o1.child = null;
    }, 1000);

    return o1;
})();

Are objects o2 and o3 eligible for garbage collection after timeout callback exits?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

4

With this code running in a function, the local variables will be out of scope and the objects pointed to will be available for garbage collection after the timeout fires and breaks the chain linking them, so only o1 will remain, as assigned to o.

If however you just had the contents of the function running in the global scope, eg:

var o1 = {name: "o1"};
var o2 = {name: "o2"};
var o3 = {name: "o3"};

o1.child = o2;
o2.child = o3;

setTimeout(function() {
    o1.child = null;
}, 1000);

then o1, o2 and o3 will all be properties of the global object (window in browsers) and would need to be cleared individually.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • 2
    @Maximus That's changed the scope of the question somewhat... you should generally avoid edits like that – James Thorpe Apr 14 '16 at 14:50
  • yeah, sorry for that, the example wasn't correct. The current one is correct. Can you answer based on the new example? – Max Koretskyi Apr 14 '16 at 14:51
  • 1
    _all the local variables will be out of scope and available for garbage collection_ - the problem here is that `o1` has reference to `o2` through its child property, that what concerns me. – Max Koretskyi Apr 14 '16 at 14:54
  • @Maximus By setting `o1.child` to `null`, you're breaking that chain though? – James Thorpe Apr 14 '16 at 14:55
  • yes, just wanted to make sure you've seen that :) because that's the gist of the question, not whether variables are eligible for gc when function exists :) – Max Koretskyi Apr 14 '16 at 14:56
  • @Maximus Note I said they're eligible for collection when the timeout fires, not when the function exits – James Thorpe Apr 14 '16 at 14:57
  • @HermanVanDerBlom Garbage collection [absolutely exists](http://stackoverflow.com/questions/864516/what-is-javascript-garbage-collection) in modern JS engines. See also [the MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#Garbage_collection). – James Thorpe Apr 14 '16 at 15:00
  • one more clarification, since js uses `nongenerational mark-and-sweep garbage collector` the objects will be eligible for gc even if they had circular reference to each other like in my updated code, correct? – Max Koretskyi Apr 14 '16 at 15:28
  • 2
    @Maximus Yes, they'll still be eligible as there are no other references to them. Note however that different JS engines (or even different versions of the same engine) could easily use different GC implementations. – James Thorpe Apr 14 '16 at 15:29
  • I see, thanks) Wondering why I got two downvotes) Is this silly question? – Max Koretskyi Apr 14 '16 at 15:36
  • @Maximus No idea - seems a perfectly valid Q to me! – James Thorpe Apr 14 '16 at 15:36
  • @Maximus Perhaps clarify the "within 1 second" bit - do you mean are they eligible before the timeout is up because the variables drop out of scope or are you asking if they're eligible after the timeout has expired? (either way the answer is "after the timeout is up", as in my answer) – James Thorpe Apr 14 '16 at 15:38
  • yeah, clarified that – Max Koretskyi Apr 14 '16 at 15:39