Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Another GC question

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
public Object method() {
Object o = new Integer(10); //line 1
Object [] oa = new Object[1]; //line 2
oa[0] = o; //line 3
o = null; //line 4
return oa[0]; //line 5
}
When is the Integer object, created in line 1, eligible for garbage collection?
A. Just after line 3.
B. Just after line 4.
C. Never in this method.
D. Just after line 5 (that is, as the method returns).
I would say given the choices. The answer should be C. Because we just dont know how the object being returned will be handled. If it is assigned to a reference then it will NOT be eligible for GC.
Am I correct? Though the answer given is B. I dont think this is right because the array's 0th index is still pointing to the Integer object.
Thanks.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Cathy -
I agree. You have a reference to the object at oa[0], so it shouldn't be eligible for gc.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you both... no one know when will this object be illigible for GC... One can say for sure though that the array object oa created at line 2 is illigible for GC after exiting the method...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody,
I think that b is the right answer because both o Object reference and oa[0] after line x
are pointing to the same object so when I am nulling reference on line x than both o and oa[0]
have a null value.
The object they were pointing to is unreacheable to anything so it can be garbage collected.
After the method has completed oa[0] reference could be assigned to any legal variable but the object it was referring still remain unreacheable.
I am not completely sure of this but this kind of thinking sound reasonable to me.
Please let me know if you agree with this.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gabriel, the B option is clearly wrong, and here is why:
On line 1 a new Integer object is created and reference to it is assigned to o. On line 3 that reference is copied to the oa[0]. On line four the reference o is nullified. This does not, how ever, affect oa[0] in any way. Only the reference is nullified, not the object behind the reference.
 
Gabriel Gigliotti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, now I have understood I have clearly made a mistake. Thanks for your kind explanation.
Ciao
Gab
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic