• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt with garbage collection  RSS feed

 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an example (8.1) from Khalid Mughal:



The book said that the possible output from the program would be:

Return from creating list X
Return from creating list Y
X: item3: recycled
X: item2: recycled
X: item1: recycled
//--------etc etc----------//

---------
Question:
---------
I understood that the reference "list" at LINE 21 is now refering to the "Y" object, and so item3, item2 and item1 created by createList("X") would loose their referers, and hence are eligible for garbage collection. But then, when createList("Y") creates item3, item2 and item1, I only see item1 being returned. In other words, the reference "list" is now refering to the HeavyItem object created by the reference item1. I don't see any need for item3 and item2, nor do I see any references holding onto item3 or item2, unless of course -- which obviously I am -- missing something.
Please correct me.

Thanks,
-Vijay
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The code seems to be trying to create a linked list with three nodes for each list. However, the second argument to the final two node constructors are null, whereas they should refer to the preceeding objects. Anyways, the output should be as stated.

_steve.
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Steve Simmon,

But what is still not clear is the fact that the two nodes have no references holding on to them, other than item3 and item2. And item3 and item2 in turn, do not have any references holding on to them. Could you explain why?

Thanks,

-Vijay
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!