Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens if you dont close a InputStream  RSS feed

 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through some code that I will have to maintain in the future when I came across this piece of code


But the input stream s is never closed. This is a standalone Java programs and there are several such programs where the InputStream is never closed.

My question is can this potentially cause any trouble and is it necessary to really change the code to close the InputStream? The program seems to have worked till now

Thanks,
Raj
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15859
80
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally the file will be closed when the InputStream object is garbage collected or when the program ends.

It's normally not a good idea to leave things open like this. Add a statement s.close() after properties.load(s).
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a statement s.close() after properties.load(s).


Thats what I want to but doing that may be more work than its worth. These are all Java batch program that are called by a CNTR M scheduler

To do the change
1. I'll have to raise a CR, which then have be approved
2. All the classes may have to be tested (Dont know if some freak demands it but I will atleast be forced to prepare a unit test case document by my manager) not sure if JUNIT test cases are there
3. The usual customary code code check in, review, build process

Normally the file will be closed when the InputStream object is garbage collected or when the program ends.


That does not seem to bad a deal

So is the change still worth the effort, can something go wrong if we dont close the stream?

Thanks,
Raj
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be a lock on the file and (in some environments) you may not be able to delete the file. That exact scenario happened to me.
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
There will be a lock on the file and (in some environments) you may not be able to delete the file. That exact scenario happened to me.


I was half hoping that some thing of this sort could happen was feeling uncomfortable with that part of the code. Now I'll try raising this CR and see what happens

Thanks,
Raj
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also have resource exhaustion problems -- the OS will only let the program have a certain number of files open at once, and once you run out, the program will be unable to open any more. Garbage collection will not help here because the unable-to-open-file errors will not trigger GC.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!