wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help in garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help in garbage collection" Watch "help in garbage collection" New topic
Author

help in garbage collection

Akash Bhatt
Greenhorn

Joined: Jul 26, 2005
Posts: 25
public class GBC{

public static void gbtest(){
Vector newvec=new Vector();
///code that uses newvec
// use of newvec over
}
}


Now do we need to set newvec=null explicitly in the method gbtest or since its a local variable it would be destroyed(as it lives on stack) and object refrenced by it would be automatically collected from the heap by gc?

Thanks in Advance
AKASH


...ToucH SkieS
AKASH
SCJP 1.4 86%
SCWCD 1.4 82%
Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
We cannot be sure that the vector object will be garbage collected unless we know what lies inside the commented piece of code. It may be possible that the rest of the code in the method makes other variables point to the object referred to by var 'newvec'. In that case, vector object will not be garbage collected as it still has live references pointing to it.
I hope, this helps.


SCJP 1.4 - 95%
Akash Bhatt
Greenhorn

Joined: Jul 26, 2005
Posts: 25
no its sure that the there no other refrences in the code then??
Sharn Arora
Ranch Hand

Joined: May 09, 2006
Posts: 57
Object referred to by var 'newvec' will be eligible for garbage collection the moment it goes out of scope. There is no need to explicitely set it to null.

Following is the snippet from sun java docs:

'An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.'

Hope this helps.
Akash Bhatt
Greenhorn

Joined: Jul 26, 2005
Posts: 25
Thanks Sharn that clears all my doubts.. nyways could you please provide the link to those good sun java docs ... ...

Thanks in Advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help in garbage collection
 
Similar Threads
Invoking method in packaged class
Old Collections Syntax in java 1.5
corba idl sql resultset java.util.vector
GUI - tile
OO - where to start