• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

GC

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class Kal{
2. public object Haq(){
3. Object obj=new Float(2.14F);
4. Object [] obj1=new Object[1];
5. obj1[0]=o;
6. obj=null;
7. return obj;
8. } // end Haq() method.
9. }
When is the Float object creation in line 3 eligible for garbage collection ?
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaleem,
Kindly refer to your code. It will not compile due to error in line # 5 and the return type of the haq should be "Object" and not "object". I have changed the line # as <code>obj1[0]=obj;</code>
Anyway, what I understand from the code. The Float object pointed by reference "obj" will be garbage collected only after the return from the function. The reason is that at line # 5 the Float object (2.14F) can be accessed by two refernces viz., obj and obj1[0]. After the execution of line # 6. The connection to Float(2.14F) is de-refferenced by the obj. But, the object can still be reached by the reference obj1[0], so the same is still not eligible for GC. At line # 7 you are returning the object's reference to the callee function, so "null" is returned. Finally, at line # 8, the function returns so , the GC must release all the locally created object's that are unreacheable , in the current code this means that Float(2.14F) object must be GC'ed.
Hope this clears your doubt.
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited September 27, 2001).]
 
Kaleem Haqqi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ravindra Mohan!! Its clear now!!

------------------
-Kaleem Haqqi
Java is mind work. Having the right frame of mind is essential.
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic