db.Size()
function returns lsm, vlog file’s sizes. Its comment says It can be used to decide how often to call RunValueLogGC. How lsm and vlog file sizes will tell how often to do GC?
1 Like
package main
import (
"fmt"
"log"
"time"
"github.com/dgraph-io/badger/v3"
)
func main() {
// Open the Badger database
opts := badger.DefaultOptions("").WithInMemory(true)
db, err := badger.Open(opts)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Run a ticker to periodically check the size and run GC
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for range ticker.C {
lsmSize, vlogSize := db.Size()
fmt.Printf("LSM Size: %d bytes, Vlog Size: %d bytes\n", lsmSize, vlogSize)
// Define a threshold for when to run GC
const gcThreshold int64 = 100 * 1024 * 1024 // 100 MB
if vlogSize > gcThreshold {
fmt.Println("Running value log GC...")
err := db.RunValueLogGC(0.5) // 0.5 is the discard ratio
if err != nil {
if err != badger.ErrNoRewrite {
log.Printf("Error running GC: %v\n", err)
}
} else {
fmt.Println("GC completed successfully")
}
}
}
}```