Ensure GC runs after each compactor call

GC is triggered rarely, which may cause unnecessarily high memory
spikes when running several compaction cycles in a row. Explicitly run
GC so we don't have idle bytes marked as used from the previous cycle.
This commit is contained in:
Fabian Reinartz 2017-03-21 12:21:02 +01:00
parent 789e8224ff
commit 70909ca8ad

10
db.go
View file

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -285,6 +286,7 @@ func (db *DB) compact() (changes bool, err error) {
return changes, errors.Wrap(err, "persist head block") return changes, errors.Wrap(err, "persist head block")
} }
changes = true changes = true
runtime.GC()
} }
// Check for compactions of multiple blocks. // Check for compactions of multiple blocks.
@ -293,6 +295,9 @@ func (db *DB) compact() (changes bool, err error) {
if err != nil { if err != nil {
return changes, errors.Wrap(err, "plan compaction") return changes, errors.Wrap(err, "plan compaction")
} }
if len(plans) == 0 {
break
}
select { select {
case <-db.stopc: case <-db.stopc:
@ -309,10 +314,7 @@ func (db *DB) compact() (changes bool, err error) {
return changes, errors.Wrapf(err, "compact %s", p) return changes, errors.Wrapf(err, "compact %s", p)
} }
changes = true changes = true
} runtime.GC()
// If we didn't compact anything, there's nothing left to do.
if len(plans) == 0 {
break
} }
} }