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

If I open and close the socket by calling an instance

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

Linux claims that this socket is still open, or at least a file descriptor to connect to. When requesting open files for this lsof process, there is an entry for a 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 is saved until the program is closed. Is there any other way to permanently close a socket? I am a little worried that my java application might block many file descriptors. Is it possible? Or does Java support these sockets to reuse them even if ReuseAdress is installed?

+6
java linux file-descriptor sockets
source share
5 answers

If these sockets are in TIME_WAIT state, this is normal, at least for a while. Check it out with netstat; for sockets, there are often several minutes to ensure that peeling data from the socket is successfully selected before reusing the port for the new socket.

+5
source share

You can also check /proc/<pid>/fd , the directory will contain all of your open file descriptors. If the file disappears after the socket is closed, you will not encounter any problems (at least not with file descriptors :).

+3
source share

I think this is not your problem with the program.

In SUN_Java, when the native bootable socket is loaded, MADIC_SOCK fd is created.

writing to MAGIC_SOCK will result in a Connect Rest Exception, and reading at MAGIC_SOCK will result in EOF.

peer magic_sock has been completely closed, and magic_sock itself is half_closed, and the state will remain "cannot identify the protocol."

+1
source share

Maybe this is a socket of some other protocol ("Unable to determine the protocol, eh?), Used internally in the implementation to do something that is created on the first socket.

Have you tried repeatedly creating sockets and closing them to make sure that these sockets are really saved? It seems likely that this is a one-off.

Java probably uses sockets internally for a lot of things - it could be Unix, Netlink (under Linux), or some other type of socket.

0
source share

Create a small bash script to track open sockets for a specific application or pid, and let it run while testing your java application.

In any case, I doubt that there are any leaks in this thing, since sockets are very used in the linux / unix world, and this problem will develop very quickly

0
source share

All Articles