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

garbage collection

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all..
I was just reading abt garbage collection in Marcus's notes. i've a small doubt regarding objects .. like when will they be eligible for garbage collection. Suppose , we have a method like..
public boolean aMethod()
{
Object ob = new Object();
// some code
ob = null;
return false;
}
now, when the object ob will be eligible for garbage collection?
Is it after the statement ob = null or when the method returns?
Please clarify...
Thank U
Mary.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AS soon as you set obj=null, the object being referenced by obj is eligible for garbage collection. However the JLS does NOT specify how garbage collection is to be implemented. Hence some JVM's may choose to garbage-collect the object after the method returns, while some may immediately garbage-collect it. From the stand-point of the SCJP, the object is eligible for gc as soon as you set it's reference to null.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I just had a question about this question. I understand that an object will be garbage collected when it equal null. At the end of a method, assuming the local object is reference on the last line of the method. Will the object in the method, be eligible for garbage collection when the method exits, since it is a local object.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object will be eligible for GC as soon as the reference count for that object becomes 0 (zero)
In the give code, object will be eligible after line ob = null; it executed, but there is no way to know that when the object is extracted collected, it may be collected any time after it becomes eligible for GC.
Object o1 = new Emp(); //1
Object o2 = o1; //2
o1 = null; //3
In this case, object refered by o1 will not be eligible after line 3, b'cos variable o2 is still refering that object.
Object declared in method :
Object declared in method as local variable will be eligible for GC, after that method completes execution. sucj local object have validity in scope of method.
class Q1
{
void aMethod()
{
Emp e1 = new Emp(); //1
//few lines of code here
//2
}
}
In this example, object refered by object variable e1, is eligible for GC after line 2, i.e. when aMethod() completes execution.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic