Say we have an opened SocketChannel
. Is it important to explicitly close it before terminating an application? In other words, if we don't do that, is any risk of leaving unclosed system resources?

- 34,031
- 20
- 135
- 241
-
well when you invoke `close`, it also closes the `Socket` and I think this is something that you might want – Eugene Sep 12 '18 at 09:49
-
You probably wont create resource leak as JVM destruction will release socket anyway. But results might vary on other side - eg stream interuption vs stream close. – Antoniossss Sep 12 '18 at 11:09
2 Answers
Assuming that your OS is a modern multi-user operating system, there is no risk that exiting a process without closing channels, dockets, file descriptors will leave resources unclosed. All resources owned by a user-space process are handled appropriately (i.e. closed if necessary1) by the OS when the process exits.
This applies when the user process is a JVM running a Java application, as well as to any other kind of user process.
1 - It is a bit more complicated than "everything is closed". For example, in UNIX / Linux a child process can inherit open "file" handles from its parent, so it is conceivable that two or more user processes could be sharing a network socket. The OS will handle this scenario with reference counts.

- 698,415
- 94
- 811
- 1,216
The socket will close automatically when your process terminates and all the process' resources will be freed.
BUT if this is a TCP socket, then the other end will not generally be notified when this happens. It'll be just like someone unplugged the network cable.
If you're communicating between processes on the same machine, then you may want to use pipes instead, which provide reliable notification to the other end when your end is automatically closed.
If you want to spawn a process and have it die when the parent goes down, I suggest using an unnamed pipe to the child process stdin for that. It's the easiest thing and it's very reliable.

- 53,709
- 3
- 46
- 87
-
1TCP _does_ notify the peer at least on the OSes where JavaSE runs: if any process including JVM just exits, Unix sends FIN (like `shutdown()`) unless you have set Linger then it sends RST; Windows sends RST. For a smartcard or something like that, maybe not. – dave_thompson_085 Sep 13 '18 at 08:34
-
That has not been my experience for abruptly terminated processes, specifically on Windows. Do you have a ref? I still won't really believe it, but it could help the OP make a decision :-) – Matt Timmermans Sep 13 '18 at 12:10
-
I'm pretty sure I've seen some Unix spec on this, but _long_ ago, and I can't find any handy one now; I don't think I ever even _looked_ for an MS spec, just noticed it was different and thought "as usual". But I've executed this case thousands of times over many years, some intentional (testing error handling logic) and many unintentional (something died due to a bug and other code or scaffolding saw it). Albeit not on W10 (I reject that) and conceivably that could differ. Feel free to get tcpdump or wireshark or whatever is applicable and try it. – dave_thompson_085 Sep 14 '18 at 02:10