Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Is it necessary to close stream in this case?

 
Ranch Hand
Posts: 430
  • 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!
 
Rancher
Posts: 43016
76
  • 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: 430
  • 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.

 
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: 430
  • 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().

reply
    Bookmark Topic Watch Topic
  • New Topic