This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Xolile Nxuseka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch! Please => QuoteYourSources, => ShowSomeEffort, and => UseCodeTags when you post a code snaps, it will be more readable.
 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for my own sanity:

 
Hrishikesh Yeshwant Alshi
Ranch Hand
Posts: 62
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, two objects can be GCed.
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep. 2.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, answer is 2, but fir these kind of questions, draw diagrams with pencil. You can count it then.
 
Mukunda s Dhakate
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why 2???
 
Matthew Raw
Ranch Hand
Posts: 76
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic