Bug 62432

Summary: Memory Leak in Statement Finalizer?
Product: Tomcat Modules Reporter: riegerm <mri>
Component: jdbc-poolAssignee: Tomcat Developers Mailing List <dev>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: proposal for StatementFinalizer with regular cleanup of statements list

Description riegerm 2018-06-06 14:23:06 UTC
Created attachment 35956 [details]
proposal for StatementFinalizer with regular cleanup of statements list

The StatementFinalizer keeps a list of statements which he closes
if they're still open when the connection is closed. The list keeps
weak references to the statements, so when the statement is closed
by client code, it can be garbage collected and will not be closed
by the StatementFinalizer.

However, the StatementFinalizer keeps the references to the Statements
indirectly through instances of StatementFinalizer$StatementEntry.
These instances are not cleaned up even when the statements they refer
to have long been closed. Mostly this does not seem to be a problem when
connections are closed quickly.

But we have had long running jobs that created many Statements with
the same connection where the instances of (empty) 
StatementFinalizer$StatementEntry finally brought the VM to its knees.

The useless instances of StatementFinalizer$StatementEntry kept
around in the statement list constitute a memory leak in my opinion.

The attached proposal invokes a "cleaner" method every time before a
new statement is added to the list. This method removes empty entries
from the list.
If this linear sweep of the statements list for each addition seems
an excessive loss of performance, one could imagine "throttled" invocations
(every nth time).