• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Number of objects eligible for GC

 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred and Matthew!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i also agree with you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic