aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many Objects are elligible for Gabbage Collection at Line 17? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many Objects are elligible for Gabbage Collection at Line 17?" Watch "How many Objects are elligible for Gabbage Collection at Line 17?" New topic
Author

How many Objects are elligible for Gabbage Collection at Line 17?

Xolile Nxuseka
Greenhorn

Joined: Apr 14, 2009
Posts: 9

1. class A{
2.
3. A a1;
4. A a2;
5.
6. public void finalize(){
7. System.out.println("-");
8. }
9.
10. public static void main(String args[]){
11. A s1=new A();
12. s1.a1=new A();
13. s1.a2=s1;
14. s1.a1.a2=new A();
15. s1.a1.a2.a2=new A();
16. s1.a1.a2.a2.a1=s1.a1;
17. s1.a2.a1.a2=null;
18. System.gc();
19. }
20.
21. }
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Welcome to JavaRanch! Please => QuoteYourSources, => ShowSomeEffort, and => UseCodeTags when you post a code snaps, it will be more readable.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

Just for my own sanity:



Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62

In my opinion, two objects can be GCed.
Rikesh Desai
Ranch Hand

Joined: Jun 02, 2010
Posts: 83
yep. 2.


OCPJP 95%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Yes, answer is 2, but fir these kind of questions, draw diagrams with pencil. You can count it then.
Mukunda s Dhakate
Greenhorn

Joined: Oct 29, 2007
Posts: 12
why 2???
Matthew Raw
Ranch Hand

Joined: Nov 30, 2010
Posts: 76

I agree with Abimaran. You can solve these types of questions easily if you draw a diagram of what is happening. I draw usually the given objects in circles and then I draw a arrow pointing to that circle. The arrows thus begins with the reference variable name and ends with the circle which represents the object. Then I delete and move the arrows as the code follows and at the desired line, I count the circles to which there are no arrows pointing to. This is the number of objects eligible for GC. I hope I wrote it understandably :-)
 
jQuery in Action, 2nd edition
 
subject: How many Objects are elligible for Gabbage Collection at Line 17?