Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Garbage collection - island of isolation

 
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I came across this question:


When line 18 is reached, how many objects will be eligible for garbage collection?


I can not understand how only 1 object is  eligible for garbage collection.
a1, b1 and b1 is set to null. then a2 still pointing to his object, and have a b2 reference pointing to b2 object.
so a1, b1 and b1 are unreachable and should be eligible for garbage collection?

(also I add an image for example, please correct me if there is a mistake)

Thanks
output_WOwSNg.gif
[Thumbnail for output_WOwSNg.gif]
 
Maor Pau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:
Does that mean you are good now? Or that those posts didn't answer your question?


Still did not understand  
Content minimized. Click to view
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maor Pau wrote:
Still did not understand  



Well, perhaps you can elaborate a bit? Showing us a bunch of topics (which I assumed are resolved) doesn't explain why you still don't understand.

BTW, I really like your image. It does a really good job at explaining why only one object is eligible for garbage collection.

Henry
 
Master Rancher
Posts: 4671
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
BTW, I really like your image. It does a really good job at explaining why only one object is eligible for garbage collection.

Henry



I think it shows why Maor thinks more than 1 is eligible, because that diagram makes b1 in the Alpha class look like an instance variable.
 
Maor Pau
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is, static variable b1 in the Alpha class cannot be eligible for garbage collection while the class is loaded
even though there is no reference available to a1, b1 and b1 (from outside).

btw this question took from OCA/OCP Java SE 7 Programmer I & II Study Guide by Kathy Sierra
 
Marshal
Posts: 25838
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maor Pau wrote:My question is, static variable b1 in the Alpha class cannot be eligible for garbage collection while the class is loaded...



I don't know if you saw this advice in any of the other garbage-collection threads you read, but here it is anyway: Variables are never eligible for garbage collection. Garbage collection only operates on objects. So when you identify a variable with an object which it refers to, as you did there, you can be leading yourself down the wrong path.

So when you draw your diagram (which is a good way of approaching these problems) it's a good idea to give separate names (or numbers) to the objects in the diagram. Doing that removes the confusion which can occur when two or more variables refer to a single object, and the confusion which can occur when a variable refers to one object and is then changed to refer to a different object.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic