• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it necessary to close stream in this case?

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 261
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic