Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GC question (code)

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

The question is from SCJP 6 study guide, anyone can tell me what is the answer, and why. Thanks
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Java Ranch!

It would make your code more readable if you used code-tags.
 
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wu Wen,

Welcome to Javaranch.
According to me 2 objects are eligible for garbage collection.
There are total 4 objects are created in this code.
These 2 are objects referred by a1 and b1.
The object referred to by b2 is not eligible for garbage collection since it is still referred by a2.b2.
 
Greenhorn
Posts: 25
Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think answer would be 1
because b1 is actually Static reference variable in class Alpha. So even if we do a1=null still the object exist and we can access by Alpha.b1. So only one object is garbage collected which is referred by a1.
Correct me is if i am wrong.

Regards
Ankit Maini
 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankit maini wrote:I think answer would be 1
because b1 is actually Static reference variable in class Alpha. So even if we do a1=null still the object exist and we can access by Alpha.b1. So only one object is garbage collected which is referred by a1.
Correct me is if i am wrong.

Regards
Ankit Maini




This is the right answer. I got this question from java 6 study guide book, chapter 3 question 11. Thank you for your explanation.
 
Jagdev Singh
Ranch Hand
Posts: 71
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I missed the that b1 is an static variable.

nice question.
 
Ranch Hand
Posts: 47
Netbeans IDE Eclipse IDE Debian
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wu Wen,

I am also studying for scjp and I have found a way to always responds gc questions with 100 %.

To do this you draw two regions, a stack and a heap. in the stack you write the object references and in the heap the object (with static if so). Then draw a line between reference and object. when references are invalid remove the line. Move to the code line by line adding references and objects and/or lines going from stack to heap. Objects without lines are available for GC.

Hope this helps.

Regards,
Ben

 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Ooms wrote:Hi Wu Wen,

I am also studying for scjp and I have found a way to always responds gc questions with 100 %.

To do this you draw two regions, a stack and a heap. in the stack you write the object references and in the heap the object (with static if so). Then draw a line between reference and object. when references are invalid remove the line. Move to the code line by line adding references and objects and/or lines going from stack to heap. Objects without lines are available for GC.

Hope this helps.

Regards,
Ben



Thank you for the tip!
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Ooms wrote:Hi Wu Wen,

I am also studying for scjp and I have found a way to always responds gc questions with 100 %.

To do this you draw two regions, a stack and a heap. in the stack you write the object references and in the heap the object (with static if so). Then draw a line between reference and object. when references are invalid remove the line. Move to the code line by line adding references and objects and/or lines going from stack to heap. Objects without lines are available for GC.

Hope this helps.

Regards,
Ben




thanks Ben
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic