fusesource / leveldbjni

A Java Native Interface to LevelDB
BSD 3-Clause "New" or "Revised" License
536 stars 143 forks source link

Memory leak with pooled memory #16

Closed rgrzywinski closed 11 years ago

rgrzywinski commented 12 years ago

We have been experiencing memory leaks when using pooled memory. The problem resides in NativeBuffer.Pool.create() (along with its interaction with the delete() and release() methods). Consider the case where there are two create() methods called before the corresponding delete()s are called (as is the case in NativeDB.put()) and the total size of the key-value is less than the chunk size (but each of the key and value individually are smaller than the chunk size). It is possible for the first create() to create an allocation (via NativeBuffer.Pool.allocate()) but the second create() to trigger remaining < size and have delete() called.

The memory leak occurs because delete() calls allocation.release() which will not actually release the memory because the NativeBuffer from the first create() still has a reference to it but yet self is set to 0! This means that when the reference counter finally gets to zero that nothing is actually free'd since the pointer (self) isn't pointing to the allocated memory any more.

At this point, I'd wager that the fix is to simply move self = 0 into else if block above where it is now.

chirino commented 11 years ago

Looks like #17 fixes this issue.