File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with garbage collection

 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources and UseCodeTags.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic