Locked in garbage collection?

Is the close() method of the Closable interface called, and the Closable object Closable garbage collection? [in java 6.0]

I have a static variable that is a resource (database connection). Since this is a static resource, there is no right place to explicitly call close() .

+4
source share
4 answers

The quick answer is no. GC doesn't care about Closeable .

Java has a protected void finalize() throws Throwable { } method that you can override and it will be called in the GC. This is a kind of work, for example. in FileInputStream :

 /** * Ensures that the <code>close</code> method of this file input stream is * called when there are no more references to it. * * @exception IOException if an I/O error occurs. * @see java.io.FileInputStream#close() */ protected void finalize() throws IOException { if ((fd != null) && (fd != FileDescriptor.in)) { /* * Finalizer should not release the FileDescriptor if another * stream is still using it. If the user directly invokes * close() then the FileDescriptor is also released. */ runningFinalize.set(Boolean.TRUE); try { close(); } finally { runningFinalize.set(Boolean.FALSE); } } } 

The problem is that it creates more problems than it's worth: for example, the JVM does not guarantee that it will ever call this method. That is, you should never use it to process resources; what you see above is a security network to make file handler leaks less damaging.

Another problem will be that the static field will not collect garbage , i.e. while your class will be visible. Thus, you have no way to use finalization.

However, you can use Runtime.addShutdownHook() - it will add another layer of security networks to your application, giving you the ability to correctly close the connection when you exit. Given that you are using a static field, the lifetime of your connection is likely to be the same as for the JVM.

I would recommend considering the approach, however.

+5
source

Perhaps you could use finalization like this ?

+1
source

It depends on the implementation of the Closeable interface, how it wants to handle garbage collection. FileInputStream , for example, implements the Object # finalize () method to call the Closeable # close () method.

0
source

If you do not close the connection, this will lead to a leak in the connection memory; unless the application server / web server is shut down. You can close all of your connections by calling the private method or use finalilize

 public void closeConnection { try { rs.close(); } catch (Exception e) { // TODO Auto-generated catch block } try { ps.close(); } catch (Exception e) { // TODO Auto-generated catch block } try { conn.close(); } catch (Exception e) { // TODO Auto-generated catch block } } 

...

 finally { try { rs.close(); } catch (Exception e) { // TODO Auto-generated catch block } try { ps.close(); } catch (Exception e) { // TODO Auto-generated catch block } try { conn.close(); } catch (Exception e) { // TODO Auto-generated catch block } } 
-1
source

Source: https://habr.com/ru/post/1383797/


All Articles