If a ServerSocket is garbage-collected, what happens to its associated port binding?

java, networking, serversocket

Solution

I'm confused: you're a "neat freak" but would rather leave socket closing to the whims of the garbage collector than have a close procedure under the control of the application in the case of a normal exit? (I assume this to be the case, because to put such a mechanism in place you would need to hold a reference, thus eliminating your perceived problem.)

For what it's worth, I don't think that there'll actually be a problem in practice if you don't hold on to the reference:

- internally, it is likely that a reference will be held while the socket is bound

- it is likely that the ServerSocket's close() method will be called on garbage collection. It is called in the finalize() method of AbstractPlainSocketImpl, so in principle will be called, with the proviso that there is no guarantee that any finalize method will actually get called.

- if the application terminates abnormally, there is no guarantee that the socket will be unbound immediately, though on recent O/Ses it is likely to be (best to test this).

However, I would really recommend coding a "clean" shutdown mechanism for the case where your app is shut down cleanly, and for that you will need to hold on to a reference to the socket. So I think you're really inventing a problem for yourself that need not exist if you just use sensible programming practice instead.

Problem

I want to create an application that can detect other instances of itself and prevent them from running. To accomplish this, I'm thinking of opening up a new `ServerSocket` on some application-specific port, and then relying on the exception that should be thrown if I try to bind to the same port more than once to "detect" and kill duplicate application instances. I know I could do something like write a file into the present-working-directory and "detect" it to accomplish the same sort of behavior, but I really don't want to do this (what happens if the app dies and can't remove the file?), so that's why I've chosen the `ServerSocket` route. Let's say I have the following code: ``` public class MyClass{ public static void main(String[] args) throws IOException{ new ServerSocket(1234); new Thread(){ //This is a non-daemon thread. Assume its run() method never returns. }.start(); } } ``` Question Short of creating the `ServerSocket`, my application never really needs to use it again, because its mere existence allows me to detect when another instance of my app attempts to start up. Thus, saving a reference to said `ServerSocket` will result in a compile warning (unused reference). I'm a neat-freak, so I'd rather not save a reference if I can avoid it. My question is, will this `ServerSocket` instance get garbage collected before all non-daemon threads exit (assuming the app doesn't fail or exit some other way), and if so, will its associated port become unbound as a result?

Original source