alexsporn commented :
Hi @jarifibrahim, Luca told me to post our findings here so you can take a look.
Our database folder is about 300GB big. Just opening the database consumes 13GB ram with the settings we are using.
I ran the following “minimal badger” using our settings that just opens the database and runs the garbage collector every 5 minutes.
package main
import (
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
)
var (
deathWaitGroup = &sync.WaitGroup{}
memUpdateTicker *time.Ticker
cleanupTicker *time.Ticker
tickerQuitSignal chan struct{}
instance *badger.DB
once sync.Once
directory = "mainnetdb"
)
func main() {
deathWaitGroup.Add(1)
printMemoryStats()
db := getBadgerInstance()
printMemoryStats()
memUpdateTicker = time.NewTicker(1 * time.Minute)
cleanupTicker = time.NewTicker(5 * time.Minute)
tickerQuitSignal = make(chan struct{})
go func() {
for {
select {
case <-tickerQuitSignal:
return
case <-memUpdateTicker.C:
printMemoryStats()
case <- cleanupTicker.C:
cleanupBadgerInstance()
}
}
}()
go gracefullyDies()
deathWaitGroup.Wait()
fmt.Println(time.Now(),
"[Badger]",
"Closing badger")
db.Close()
printMemoryStats()
fmt.Println(time.Now(),
"Bye!")
}
func printMemoryStats() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Println(time.Now(),
"[Memory]",
"Sys:", m.Sys, ",",
"HeapAlloc:", m.HeapAlloc, ",",
"HeapSys:", m.HeapSys, ",",
"HeapInuse:", m.HeapInuse, ",",
"HeapIdle:", m.HeapIdle, ",",
"HeapReleased:", m.HeapReleased, ",",
"HeapObjects:", m.HeapObjects, ",",
"MSpanInuse:", m.MSpanInuse, ",",
"MCacheInuse:", m.MCacheInuse, ",",
"StackSys:", m.StackSys, ",",
"NumGC:", m.NumGC, ",",
"LastPauseGC:", m.PauseNs[(m.NumGC+255)%256],
)
}
func gracefullyDies() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
sig := <-ch // waits for death signal
fmt.Println(time.Now(),
"Caught signal '", sig, "': shutting down. Please wait...")
deathWaitGroup.Done()
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func checkDir(dir string) error {
exists, err := exists(dir)
if err != nil {
return err
}
if !exists {
return os.Mkdir(dir, 0700)
}
return nil
}
func getBadgerInstance() *badger.DB {
once.Do(func() {
fmt.Println(time.Now(),
"[Badger]",
"Initialize badger at", directory)
if err := checkDir(directory); err != nil {
panic(err)
}
var opts badger.Options
opts = badger.DefaultOptions(directory)
opts.Logger = nil
opts.LevelSizeMultiplier = 10
opts.TableLoadingMode = options.MemoryMap
opts.ValueLogLoadingMode = options.FileIO
opts.MaxLevels = 7
opts.MaxTableSize = 67108864
opts.NumCompactors = 1
opts.NumLevelZeroTables = 1
opts.NumLevelZeroTablesStall = 2
opts.NumMemtables = 1
opts.BloomFalsePositive = 0.01
opts.BlockSize = 4 * 1024
opts.SyncWrites = false
opts.NumVersionsToKeep = 1
opts.CompactL0OnClose = false
opts.KeepL0InMemory = false
opts.VerifyValueChecksum = false
opts.MaxCacheSize = 200000000
opts.ZSTDCompressionLevel = 1
opts.Compression = options.None
opts.ValueLogFileSize = 1073741823
opts.ValueLogMaxEntries = 33554431
opts.ValueThreshold = 32
opts.Truncate = false
opts.LogRotatesToFlush = 2
opts.EventLogging = false
if runtime.GOOS == "windows" {
opts = opts.WithTruncate(true)
}
db, err := badger.Open(opts)
if err != nil {
panic(err)
}
instance = db
})
return instance
}
func cleanupBadgerInstance() {
db := getBadgerInstance()
fmt.Println(time.Now(),
"[Badger]",
"Run garbage collection")
var err error
for err == nil {
err = db.RunValueLogGC(0.7)
}
}
Running on go 1.14.2 and badger 2.0.3 the output of this script is:
m=+0.000874372 [Memory] Sys: 71125000 , HeapAlloc: 206336 , HeapSys: 66813952 , HeapInuse: 663552 , HeapIdle: 66150400 , HeapReleased: 66150400 , HeapObjects: 929 , MSpanInuse: 19448 , MCacheInuse: 3472 , StackSys: 294912 , NumGC: 0 , LastPauseGC: 0
m=+0.000947733 [Badger] Initialize badger at mainnetdb
m=+23.169028430 [Memory] Sys: 13198831080 , HeapAlloc: 9874474104 , HeapSys: 12615483392 , HeapInuse: 9885589504 , HeapIdle: 2729893888 , HeapReleased: 25706496 , HeapObjects: 72432308 , MSpanInuse: 95966496 , MCacheInuse: 3472 , StackSys: 425984 , NumGC: 18 , LastPauseGC: 70260
m=+83.169241833 [Memory] Sys: 13618802200 , HeapAlloc: 8512172064 , HeapSys: 13018562560 , HeapInuse: 8519155712 , HeapIdle: 4499406848 , HeapReleased: 138412032 , HeapObjects: 63042716 , MSpanInuse: 82406072 , MCacheInuse: 3472 , StackSys: 557056 , NumGC: 18 , LastPauseGC: 70260
m=+144.188312677 [Memory] Sys: 13618802200 , HeapAlloc: 8512718048 , HeapSys: 13018071040 , HeapInuse: 8519622656 , HeapIdle: 4498448384 , HeapReleased: 138420224 , HeapObjects: 63050393 , MSpanInuse: 82414776 , MCacheInuse: 3472 , StackSys: 557056 , NumGC: 19 , LastPauseGC: 20030
m=+203.169118149 [Memory] Sys: 13618802200 , HeapAlloc: 7705851968 , HeapSys: 13018562560 , HeapInuse: 7724294144 , HeapIdle: 5294268416 , HeapReleased: 2229215232 , HeapObjects: 60794170 , MSpanInuse: 80215248 , MCacheInuse: 3472 , StackSys: 557056 , NumGC: 19 , LastPauseGC: 20030
m=+263.169157010 [Memory] Sys: 13618802200 , HeapAlloc: 7706582088 , HeapSys: 13018562560 , HeapInuse: 7724949504 , HeapIdle: 5293613056 , HeapReleased: 3845365760 , HeapObjects: 60802920 , MSpanInuse: 80223952 , MCacheInuse: 3472 , StackSys: 557056 , NumGC: 19 , LastPauseGC: 20030
m=+323.169177991 [Memory] Sys: 13618802200 , HeapAlloc: 7698655968 , HeapSys: 13018595328 , HeapInuse: 7717044224 , HeapIdle: 5301551104 , HeapReleased: 4529086464 , HeapObjects: 60794138 , MSpanInuse: 80213752 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 20 , LastPauseGC: 79151
m=+323.169231412 [Badger] Run garbage collection
m=+383.169237654 [Memory] Sys: 13619326488 , HeapAlloc: 8675305976 , HeapSys: 13018595328 , HeapInuse: 8695496704 , HeapIdle: 4323098624 , HeapReleased: 4323065856 , HeapObjects: 74470390 , MSpanInuse: 95846136 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 20 , LastPauseGC: 79151
m=+443.169400672 [Memory] Sys: 13619326488 , HeapAlloc: 7698664384 , HeapSys: 13018595328 , HeapInuse: 7717175296 , HeapIdle: 5301420032 , HeapReleased: 4322516992 , HeapObjects: 60794227 , MSpanInuse: 80199744 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 21 , LastPauseGC: 54881
m=+503.169174065 [Memory] Sys: 13619326488 , HeapAlloc: 7699394768 , HeapSys: 13018595328 , HeapInuse: 7717830656 , HeapIdle: 5300764672 , HeapReleased: 4322516992 , HeapObjects: 60802979 , MSpanInuse: 80208448 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 21 , LastPauseGC: 54881
m=+563.169219106 [Memory] Sys: 13619326488 , HeapAlloc: 7698663000 , HeapSys: 13018595328 , HeapInuse: 7717134336 , HeapIdle: 5301460992 , HeapReleased: 4528979968 , HeapObjects: 60794222 , MSpanInuse: 80209128 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 22 , LastPauseGC: 39411
m=+623.169170748 [Memory] Sys: 13619326488 , HeapAlloc: 7699393384 , HeapSys: 13018595328 , HeapInuse: 7717789696 , HeapIdle: 5300805632 , HeapReleased: 4528906240 , HeapObjects: 60802974 , MSpanInuse: 80209128 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 22 , LastPauseGC: 39411
m=+623.169202649 [Badger] Run garbage collection
m=+683.169518460 [Memory] Sys: 13619850776 , HeapAlloc: 8651915792 , HeapSys: 13018595328 , HeapInuse: 8670076928 , HeapIdle: 4348518400 , HeapReleased: 3934584832 , HeapObjects: 75124542 , MSpanInuse: 95608816 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 23 , LastPauseGC: 587954
m=+743.169230752 [Memory] Sys: 13619850776 , HeapAlloc: 8652647352 , HeapSys: 13018595328 , HeapInuse: 8670765056 , HeapIdle: 4347830272 , HeapReleased: 3934584832 , HeapObjects: 75133299 , MSpanInuse: 95626224 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 23 , LastPauseGC: 587954
m=+803.169181087 [Memory] Sys: 13619850776 , HeapAlloc: 7698667280 , HeapSys: 13018595328 , HeapInuse: 7717257216 , HeapIdle: 5301338112 , HeapReleased: 3934584832 , HeapObjects: 60794270 , MSpanInuse: 80205184 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 24 , LastPauseGC: 183622
m=+863.169221220 [Memory] Sys: 13619850776 , HeapAlloc: 7699397664 , HeapSys: 13018595328 , HeapInuse: 7717912576 , HeapIdle: 5300682752 , HeapReleased: 3934584832 , HeapObjects: 60803022 , MSpanInuse: 80205184 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 24 , LastPauseGC: 183622
m=+923.169166031 [Memory] Sys: 13619850776 , HeapAlloc: 7698666440 , HeapSys: 13018595328 , HeapInuse: 7717208064 , HeapIdle: 5301387264 , HeapReleased: 4528889856 , HeapObjects: 60794265 , MSpanInuse: 80212392 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 25 , LastPauseGC: 18920
m=+923.169234972 [Badger] Run garbage collection
m=+983.169253567 [Memory] Sys: 13621685784 , HeapAlloc: 9319352112 , HeapSys: 13018595328 , HeapInuse: 9341632512 , HeapIdle: 3676962816 , HeapReleased: 3676930048 , HeapObjects: 84584548 , MSpanInuse: 106942376 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 25 , LastPauseGC: 18920
m=+1043.169279604 [Memory] Sys: 13621685784 , HeapAlloc: 7698670656 , HeapSys: 13018595328 , HeapInuse: 7717314560 , HeapIdle: 5301280768 , HeapReleased: 3676356608 , HeapObjects: 60794309 , MSpanInuse: 80214024 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 26 , LastPauseGC: 9370
m=+1103.169239449 [Memory] Sys: 13621685784 , HeapAlloc: 7699401056 , HeapSys: 13018595328 , HeapInuse: 7717961728 , HeapIdle: 5300633600 , HeapReleased: 3676348416 , HeapObjects: 60803062 , MSpanInuse: 80214024 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 26 , LastPauseGC: 9370
m=+1163.169194404 [Memory] Sys: 13621685784 , HeapAlloc: 7698669272 , HeapSys: 13018595328 , HeapInuse: 7717273600 , HeapIdle: 5301321728 , HeapReleased: 4528840704 , HeapObjects: 60794304 , MSpanInuse: 80207224 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 27 , LastPauseGC: 38011
m=+1199.907555672 Caught signal ' interrupt ': shutting down. Please wait...
m=+1199.908137688 [Badger] Closing badger
m=+1200.259970905 [Memory] Sys: 13621685784 , HeapAlloc: 7771776192 , HeapSys: 13018595328 , HeapInuse: 7790370816 , HeapIdle: 5228224512 , HeapReleased: 4456488960 , HeapObjects: 60803175 , MSpanInuse: 80215928 , MCacheInuse: 3472 , StackSys: 524288 , NumGC: 27 , LastPauseGC: 38011
m=+1200.260005255 Bye!
The database is made out of:
282 vlog files
1896 sst files
Is there any way to reduce the base memory consumption or is this normal for a database this size?
Thanks in advance for any tips