• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Is it necessary to close stream in this case?

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().

 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic