0

Consider this multi-level nested JavaScript object.

function foo() {
 var channels = {
 2: {
 name: "station 1", 
 description: "station1",
 img: ["img1-a", "img1-b", "img1-c"]
 },
 3: {
 name: "station 2", 
 description: "station2",
 img: ["img2-a", "img2-b", "img2-c"]
 },
 4: {
 name: "station 3", 
 description: "station3",
 img: ["img3-a", "img3-b", "img3-c"]
 },
 };
 console.log(channels); 
}; 
.... 
// calling foo.
foo();

After the function foo() returns, will all the nested objects (i.e. the individual channel objects, strings, the array img, and the strings in img array, all be automatically garbage collected ?

Or, do I need to explicitly iterate through and "delete" each object?

p.campbell
101k71 gold badges265 silver badges326 bronze badges
asked Mar 15, 2012 at 21:55
3
  • 2
    There is no reference to any of those objects/arrays anymore, so they will be garbage collected eventually. Commented Mar 15, 2012 at 21:57
  • See stackoverflow.com/questions/864516/… Commented Mar 15, 2012 at 21:57
  • @j08691 thanks for providing a great reference. Commented Mar 16, 2012 at 6:37

4 Answers 4

2

Depends on what happens in console.log. Certainly in Chrome, a reference to the channels object is kept in the console, so channels cannot be GC'd.

When you remove console.log, the full channel object will properly be GC'd, because there are no other references to it.

answered Mar 15, 2012 at 21:56
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for additional clarity. I didn't think of the console.log. So, if I were to keep a reference to the above object "channels" in a closure, I assume that, the memory will be garbage collected as soon as the reference to the closure is released ? Am I right?
2

They're eligible for GC as long as nothing else is referencing them.

answered Mar 15, 2012 at 21:56

Comments

0

They should be, yes, because there is no more reference to that channels object nor the closure that contains it.

GC is mostly dependent on the browser that implements it, though, so there's no guarantee it'll actually be done. deleteing each element is overkill, though.

answered Mar 15, 2012 at 21:57

1 Comment

thanks I went with Rob's answer, due to additional info on console.log.
0

Javascript has its own garbage collector implemented by the browser's engine (v8 or whatever). You don't have to deallocate references. Once the root goes out of scope, they will all be eligible for gc.

answered Mar 15, 2012 at 21:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.