aspose file tools*
The moose likes Performance and the fly likes Is it necessary to close stream in this case? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Is it necessary to close stream in this case?" Watch "Is it necessary to close stream in this case?" New topic
Author

Is it necessary to close stream in this case?

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Hi!

After the program exits the method, the objects that are created inside the method are garbage collected. So is it necessary to close the streams?


Thank you!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
An object being GC'ed is not the same as the object being properly discarded (which would include closing all streams). Since you posted in the Performance forum, are you hoping to save time by not closing streams? If so, don't do it. The overhead will most likely be too small to even measure, and there are various stream implementations that react badly to not being closed properly.


Ping & DNS - updated with new look and Ping home screen widget
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Ulf Dittmer wrote:An object being GC'ed is not the same as the object being properly discarded (which would include closing all streams). Since you posted in the Performance forum, are you hoping to save time by not closing streams? If so, don't do it. The overhead will most likely be too small to even measure, and there are various stream implementations that react badly to not being closed properly.

thank you!
my doubt was if the streams are automatically closed when the object is gc.

Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Basically the close() method is associated with a stream to perform all the cleanup actions to free up the resources which were associated with the stream. Now you're correct in saying that when the JVM performs a GC to clean up the un-referenced stream object, the resources will get freed up then too. But, there are 2 very important things to keep in mind -
[1] as a programmer you can never be sure when the JVM does a GC
[2] by any chance if a stream object has any live references, then it will never be GC-ed!

Streams are usually resource intensive objects, and thus need to be handled in a point-to-point manner. If it weren't important to free up a stream object's resource, then the functionality included within the close() method could have very well been moved into finalize(). The very reason that the API developers thought it's important to free up a stream object's resources at the earliest, is why they moved the responsibility onto programmers to do so through the method - close() - and not leave it for the GC mechanism to do through finalize().
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
thank you!!!
Anirvan Majumdar wrote:Basically the close() method is associated with a stream to perform all the cleanup actions to free up the resources which were associated with the stream. Now you're correct in saying that when the JVM performs a GC to clean up the un-referenced stream object, the resources will get freed up then too. But, there are 2 very important things to keep in mind -
[1] as a programmer you can never be sure when the JVM does a GC
[2] by any chance if a stream object has any live references, then it will never be GC-ed!

Streams are usually resource intensive objects, and thus need to be handled in a point-to-point manner. If it weren't important to free up a stream object's resource, then the functionality included within the close() method could have very well been moved into finalize(). The very reason that the API developers thought it's important to free up a stream object's resources at the earliest, is why they moved the responsibility onto programmers to do so through the method - close() - and not leave it for the GC mechanism to do through finalize().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it necessary to close stream in this case?
 
Similar Threads
Throwable methods
store a file in tomcat server
how to send raw data to printer
Want to print a file using java program
Accessing Japanese characters from text area