• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage collector  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How many obgects are ellegible for gc?

The answer is 3 . But i dont understand why.

Have any explanation?
 
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Rafael. I made a slight tweak to the last line of your code, so the closing curly bracket is in a place consistent with your other indentations.

There's more than one kind of reference, but to help you figure out the answer to your question, lets say that an instance of a class (an "object") is eligible for garbage collection when no variable, including member variables of classes, hold a reference to that instance.

At Line 9 of your code, three instances are created. Where are their references stored? At Line 10, two new instances are created. Where are their references stored? In particular, do either of the references stored somewhere in Line 10 overwrite any of the references that were stored somewhere in Line 9? If you can figure that out, you'll be able to see how some of the instances created by Lines 9, 10, and 11 end up with no variables holding any references to them after Line 12 is executed. There will be three of them, just as you said, but you should be able, if you track which ones have their references overwritten by others, to know which ones they are.

Good luck!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!