GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes What will happen if we don't close streams or connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What will happen if we don Watch "What will happen if we don New topic

What will happen if we don't close streams or connections

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I recently saw a post in which it was described to close buffered reader or connections.
What is the importance of closing these connections & what overhead will occur if we don't close these connections?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

if you not closing the connection ,then for every user the connection will create .. remember there is a limited number of connection only . so for the next user he wont get the connection to DataBase .
you should close the connection properly
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
what will happen if we don't close the streams?
Is there any connection limitations on streams also?
If yes how can we find the limit?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39053
There is a limit, but I don't know what it is. It may vary from computer to computer. Try opening lots of file connections as Readers and see how many you can open and not close before you start getting exceptions.
Chris Blades

Joined: Dec 23, 2008
Posts: 7
The OS will close dead connections automatically, but it could take some time (relative to how fast operations should happen on a computer). A good example, though not with files (same concept though), is ports. Use a socket to connect to a port on your own computer, don't close the connection but exit the program. Immediately re-run the program and you'll sometimes get an IOException because the socket is still blocked.

The same applies to files, it's just that there can be multiple processes accessing a file at once. There is a limit though.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19697

Originally posted by Chris Blades:
but exit the program.

True, if you exit the program all (file) handles will eventually be released. The thing is, you will more likely run into problems if you don't exit the program, and keep running. Especially on servers this happens a lot. The OS doesn't know you don't need the handle any more so it keeps it open.

Also here, eventually the garbage collector will collect your objects, and if the finalize method is written correctly the handle will be released, but everybody should know that you can never predict when it runs. In fact, I once had a semi-realtime system setup that was so swamped that it crashed with a memory error. Because all other threads had a higher priority (realtime, remember) the GC never even ran.

How To Ask Questions How To Answer Questions
GeeCON Prague 2014
subject: What will happen if we don't close streams or connections