File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hi All,
Regarding garbage collection, does closing a FileInputStream object or FileOutputStream object make the object eligible for garbage collection?
Thanks.
Kezia.
bd
Greenhorn

Joined: Jan 09, 2002
Posts: 7
If you look at the documentation it states that it releases system resources. Insofar as GC is concerned if the stream object is local it should be garbage collected after the method returns - but you can explicitly set it to null (object = null) if you require it to become available for GC before the method terminates or if it is a member variable.
Also, if the method returns the stream object and it is assigned to a new varible it will persist and not be eligible for GC.
Hope this is OK.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Your name bd does not comply with the JavaRanch naming policy. We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and re-register with a name that meets these requirements.
Thanks!


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
If the FileInputStream object is local to a method and the file opened using this object is closed (the object is not set to null)long before the method terminates, would the object be eligible for garbage collection before the method terminates?
bd
Greenhorn

Joined: Jan 09, 2002
Posts: 7
If the stream object is local (and not explicitly set to null)it would not be eligible for collection until the method terminates. Try this:
CODE]import java.io.*;
public class Trial
{
public static void main (String [] args)
{
try
{
FileOutputStream fos = new FileOutputStream(new File("test.txt"));
fos.close();
//fos = null;

System.out.println(fos);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}[/CODE]
If you run it as is the println will show you that 'fos' holds a reference to the stream object created. If you uncomment the line setting it to null you will see that 'null' is printed to the command line. This makes the object it referenced eligible for GC (providing it is not referenced by anything else in your method e.g. an array etc.).
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Thanks bd.
 
wood burning stoves
 
subject: Garbage collection
 
Similar Threads
Garbage Collection doubt
Object Reference
Tech Word Game
doubt on garbage collection
GC