Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is memory leak? why java.lang.ref.Finalizer eat so much memory

I ran a heap dump on my program. When I opened it in the memory analyzer tool, I found that the java.lang.ref.Finalizer for org.logicalcobwebs.proxool.ProxyStatement was taking up a lot of memory. Why is this so?

screenshot

like image 757
fuyou001 Avatar asked Dec 02 '11 10:12

fuyou001


People also ask

What causes memory leak in Java?

In general, a Java memory leak happens when an application unintentionally (due to logical errors in code) holds on to object references that are no longer required. These unintentional object references prevent the built-in Java garbage collection mechanism from freeing up the memory consumed by these objects.

What is memory leak in Java with example?

What is a Memory Leak in Java? The standard definition of a memory leak is a scenario that occurs when objects are no longer being used by the application, but the Garbage Collector is unable to remove them from working memory – because they're still being referenced.


2 Answers

Some classes implement the Object.finalize() method. Objects which override this method need to called by a background thread call finalizer, and they can't be cleaned up until this happens. If these tasks are short and you don't discard many of these it all works well. However if you are creating lots of these objects and/or their finalizers take a long time, the queue of objects to be finalized builds up. It is possible for this queue to use up all the memory.

The solution is

  • don't use finalize()d objects if you can (if you are writing the class for the object)
  • make finalize very short (if you have to use it)
  • don't discard such objects every time (try to re-use them)

The last option is likely to be best for you as you are using an existing library.

like image 100
Peter Lawrey Avatar answered Sep 18 '22 19:09

Peter Lawrey


From what I can make out, Proxool is a connection pool for JDBC connections. This suggests to me that the problem is that your application is is misusing the connection pool. Instead of calling close on the statement objects, your code is probably dropping them and/or their parent connections. The Proxool is relying on finalizers to close the underlying driver-implemented objects ... but this requires those Finalizer instances. It could also mean that you are causing the connection to open / close (real) database connections more frequently than is necessary, and that would be bad for performance.

So I suggest that you check your code for leaked ResultSet, Statement and/or Connection objects, and make sure that you close them in finally blocks.


Looking at the memory dump, I expect you are concerned where the 898,527,228 bytes are going. The vast majority are retained by the Finalizer object whose id is 2aab07855e38. If you still have the dump file, take a look at what that Finalizer is referring to. It looks more problematic than the Proxool objects.

like image 41
Stephen C Avatar answered Sep 21 '22 19:09

Stephen C