goleveldb icon indicating copy to clipboard operation
goleveldb copied to clipboard

Memory leak

Open jayeshwebcluesglobal opened this issue 5 years ago • 4 comments

Memory is keep increasing and not getting free, pprof shows below function is allocating more and more memory

Screenshot from 2019-08-08 20-06-09

jayeshwebcluesglobal avatar Aug 08 '19 14:08 jayeshwebcluesglobal

Screenshot from 2019-08-09 20-35-47

jayeshwebcluesglobal avatar Aug 09 '19 15:08 jayeshwebcluesglobal

Memory is keep increasing

jayeshwebcluesglobal avatar Aug 09 '19 15:08 jayeshwebcluesglobal

How to modify it, I also encounter the same problem.

weihaigang avatar Sep 17 '19 03:09 weihaigang

I wonder if open() sometimes returns a cache handle and an error. If so Release() wouldn't get called in find. If so this patch (untested) might fix your problem.

diff --git a/leveldb/table.go b/leveldb/table.go
index b7759b2..bdf0c76 100644
--- a/leveldb/table.go
+++ b/leveldb/table.go
@@ -443,30 +443,36 @@ func (t *tOps) open(f *tFile) (ch *cache.Handle, err error) {
 // given key.
 func (t *tOps) find(f *tFile, key []byte, ro *opt.ReadOptions) (rkey, rvalue []byte, err error) {
        ch, err := t.open(f)
+       if ch != nil {
+               defer ch.Release()
+       }
        if err != nil {
                return nil, nil, err
        }
-       defer ch.Release()
        return ch.Value().(*table.Reader).Find(key, true, ro)
 }
 
 // Finds key that is greater than or equal to the given key.
 func (t *tOps) findKey(f *tFile, key []byte, ro *opt.ReadOptions) (rkey []byte, err error) {
        ch, err := t.open(f)
+       if ch != nil {
+               defer ch.Release()
+       }
        if err != nil {
                return nil, err
        }
-       defer ch.Release()
        return ch.Value().(*table.Reader).FindKey(key, true, ro)
 }
 
 // Returns approximate offset of the given key.
 func (t *tOps) offsetOf(f *tFile, key []byte) (offset int64, err error) {
        ch, err := t.open(f)
+       if ch != nil {
+               defer ch.Release()
+       }
        if err != nil {
                return
        }
-       defer ch.Release()
        return ch.Value().(*table.Reader).OffsetOf(key)
 }

joelschopp avatar Jan 30 '20 21:01 joelschopp