Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If the jvm gc an unclosed socket instance what would happen to the underlying tcp connection?

If there is a unused socket instance, but it is not closed.

If the jvm would gc it?

If yes ,whether the tcp connection would close automatically?

like image 250
kino lucky Avatar asked Oct 27 '25 13:10

kino lucky


1 Answers

The garbage collection will call finalize() which in turn closes the connection.

See java.net.AbstractPlainSocketImpl:

   protected void finalize() throws IOException {
        close();
    }

Almost like magic.

like image 172
Jan Groth Avatar answered Oct 30 '25 03:10

Jan Groth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!