Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seems memory leak happens when using cgo #167

Open
AaronLin98 opened this issue Nov 12, 2024 · 3 comments
Open

seems memory leak happens when using cgo #167

AaronLin98 opened this issue Nov 12, 2024 · 3 comments

Comments

@AaronLin98
Copy link

AaronLin98 commented Nov 12, 2024

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
Copy link
Owner

linxGnu commented Nov 28, 2024

@AaronLin98

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

@AaronLin98
Copy link
Author

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

@linxGnu
Copy link
Owner

linxGnu commented Dec 23, 2024

@AaronLin98

Sorry for late reply due to personal matter. Have you tried:

  • Upgrade to latest version
  • Calling writebatch.Clear() before writebatch.Destroy()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants