Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a file descriptor leak when using sockets on a linux platform?

If I open and close a socket by calling for instance

Socket s = new Socket( ... );
s.setReuseAddress(true);
in = s.getInputStream();
...
in.close(); 
s.close();      

Linux states that this socket is still open or at least the file descriptor for the connection is presen. When querying the open files for this process by lsof, there is an entry for the closed connection:

COMMAND  PID   USER   FD   TYPE DEVICE     SIZE   NODE NAME
java    9268 user    5u  sock    0,4           93417 can't identify protocol

This entry remains until the program is closed. Is there any other way to finally close the socket? I'm a little worried that my java application may block to many file descriptors. Is this possible? Or does java keep these sockets to re-use them even is ReuseAdress is set?

like image 990
tigger Avatar asked Jan 09 '09 21:01

tigger


2 Answers

If those sockets are all in the TIME_WAIT state, this is normal, at least for a little while. Check that with netstat; it is common for sockets to hang around for a few minutes to ensure that straggling data from the socket is successfully thrown away before reusing the port for a new socket.

like image 65
Richard Campbell Avatar answered Oct 10 '22 18:10

Richard Campbell


You may also want to check /proc/<pid>/fd, the directory will contain all of your currently opened file descriptors. If a file disappears after you closed the socket you will not run into any problems (at least not with your file descriptors :).

like image 24
Bombe Avatar answered Oct 10 '22 19:10

Bombe