This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Question about Garbage collection  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an exercice in K&B book ! Can anyone explain me why the right answer is B.

Thanks very much
 
Ranch Hand
Posts: 39
5
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tip: perform a dry run on this code. Draw the reference variables on the stack and the objects heap. When the reference variables are set to null, remove the link between the stack reference variable and the object on the heap. Pay special attention to the assignment on line 12! What's special about Alpha.b1? Once you are done, see how many objects are accessible through variables on the stack (in this case: a2 is the only non null varaible at the end).

Rather than tell you why, give the above a go and see if you get to the correct answer, I've given you a big hint ;)
 
Rancher
Posts: 1020
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a2 is not nulled out, so the new A instance it refers to is retained. It holds a reference to b2 through the member variable, so b2 is alive.
b1 is referred to in the static member of the class Alpha, so the only object ripe for Nirvana at this point is a1.
 
My, my, aren't you a big fella. Here, have a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!