linxGnu / grocksdb

RocksDB wrapper for Go. Support 9.x, 8.x, 7.x, 6.x, etc
MIT License
315 stars 68 forks source link

seems memory leak happens when using cgo #167

Open AaronLin98 opened 2 weeks ago

AaronLin98 commented 2 weeks ago

version: gorocksdb@v1.6.36

function: write_batch.go

func (wb *WriteBatch) Put(key, value []byte) {
    cKey := byteToChar(key)
    cValue := byteToChar(value)
    C.rocksdb_writebatch_put(wb.c, cKey, C.size_t(len(key)), cValue, C.size_t(len(value)))
}

use case:

func writeLargeObjects(db *grocksdb.DB) {
    for i := 0; i < 1000; i += 10 {
        writeLargeObjectsCore(db, i, i+10)
    }
}
func writeLargeObjectsCore(db *grocksdb.DB, l, r int) {
    largeValue := make([]byte, 1*1024*1024) // 1MB bytes
    for i := range largeValue {
        largeValue[i] = 'x'
    }

    batch := grocksdb.NewWriteBatch()
    for i := l; i < r; i++ {
        key := fmt.Sprintf("large_key_%d", i)
        batch.Put([]byte(key), largeValue)
    }

    writeOpts := grocksdb.NewDefaultWriteOptions()
    err := db.Write(writeOpts, batch)
    if err != nil {
        log.Fatalf("Write failed: %v", err)
    }
    writeOpts.Destroy()
    batch.Destroy()
}

results: By this use case, memory leak will happen. The VmRSS is much larger than Go sys memory and rocksdb-memtable and rocksdb-block-cache.

linxGnu commented 3 days ago

@AaronLin98

v1.6.36 is outdated. I highly recommend upgrading both RocksDB & grocksdb to newer version and give it another try.

AaronLin98 commented 3 days ago

I have tried the new version, it seems like cgo has some problem for memory leak