Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of objects eligible for GC

 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question posted in the "Java Developers" group on LinkedIn. I'm not the original author.

After line 14, how many objects are eligible for garbage collection?



According to the thread's author, the answer is 2, but I only count 1.

My understanding is that ob1 in Line 3 is a copy of a reference. When the reference changes at Line 5, c1 still maintains a reference to the object created in Line 11.

Therefore, the only object I see eligible for garbage collection is created in Line 05. It becomes unattached when c2 changes it's reference to c3 in Line 14.

Is my analysis incorrect?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say 3.

Since after line 14, c1, c2 and c3 all go out of scope, none of the three objects created have valid references anymore.

But I guess that depends on what exactly "after line 14" means.

Otherwise, I agree with your assessment, for whatever that is worth.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. I forgot to include the method call that occurs after line 14. It's an unknown method so I had to remove it in order to compile.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I agree with you. c1 references the C created on line 11, and c2 and c3 both reference the one created on line 13. The one created by the method call on line 12 is the only one eligible.

(It would still be one even if Java was pass by reference, but it would be a different one!).
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred and Matthew!
 
nweoo mon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i also agree with you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic