JVM abnormal exit - cleanup of system resources
I have a java program which has a server-socket listening infinitely. In case of JVM exiting abnormally through kill pid, what happens to system resources in this case, socket object ? Will the sockets be released or it will be locked up?
What is the best way to handle the cleanup of system resources like file descriptors/network-socket/jdbc connection, in case of abnormal JVM exit.
I know about finalizers, finally, but there is some amount of uncertainty and performance degradation using finalization.
Pls provide some 开发者_JAVA百科ideas
In the case of an abnormal exit, obviously the JVM can't do anything specifically to clean up whatever it has allocated. This responsibility would fall next to the operating system itself.
For sockets, modern operating systems will definitely release any sockets that had been opened by a process that crashes. The same applies to files and any other kernel objects (JDBC connections are implemented with some sort of socket).
Finalizers will not be run if the JVM terminates abnormally.
System resources like memory, file descriptors, and network sockets are released when the JVM exits. Connections to the network sockets will take a while to close due to network protocols.
JDBC connections should be released once the database becomes aware that the other end of the connection has closed. Keep-alive timers can help cleanup the connection faster. The database will cleanup open transactions, and release locks as it cleans up the connections.
精彩评论