aspose file tools*
The moose likes Beginning Java and the fly likes Problems with garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with garbage collection" Watch "Problems with garbage collection" New topic
Author

Problems with garbage collection

jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Given:
3. class Beta { }
4. class Alpha {
5. static Beta b1;
6. Beta b2;
7. }
8. public class Tester {
9. public static void main(String[] args) {
10. Beta b1 = new Beta(); Beta b2 = new Beta();
11. Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
12. a1.b1 = b1;
13. a1.b2 = b1;
14. a2.b2 = b2;
15. a1 = null; b1 = null; b2 = null
16. // do stuff
17. }
18. }
When line 16 is reached, how many objects will be eligible for garbage collection??

I personally find that 2 objects are eligible for collection, b1 and a1. However the answer has been provided as only 1object is elligible for collection!!!

Please help!!!


If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please QuoteYourSources and UseCodeTags.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
jishnu dasgupta
Ranch Hand

Joined: Mar 11, 2011
Posts: 103

Sorry for the last post!! does this helps??..by d way this one if from kathy Sierras & Bert Bates study material for scjp 1.6(TMH)




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

I personally find that 2 objects are eligible for collection, b1 and a1. However the answer has been provided as only 1object is elligible for collection!!!

Please help!!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

four objects are created - two Alphas, and two Betas.

a2 points to one of the Alpha objects. That object's b2 reference points to one of the Beta objects, and the Alpha.b1 static reference points to the other Beta object. So, of the four, three still have live references.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Don't get me started about those stupid light bulbs.
 
subject: Problems with garbage collection