From 70909ca8ad875aa6f01e1801d185b3f3bab2fda3 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 21 Mar 2017 12:21:02 +0100 Subject: [PATCH] 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. --- db.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 2901b3943..fb3fdc969 100644 --- a/db.go +++ b/db.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" "strconv" "strings" "sync" @@ -285,6 +286,7 @@ func (db *DB) compact() (changes bool, err error) { return changes, errors.Wrap(err, "persist head block") } changes = true + runtime.GC() } // Check for compactions of multiple blocks. @@ -293,6 +295,9 @@ func (db *DB) compact() (changes bool, err error) { if err != nil { return changes, errors.Wrap(err, "plan compaction") } + if len(plans) == 0 { + break + } select { case <-db.stopc: @@ -309,10 +314,7 @@ func (db *DB) compact() (changes bool, err error) { return changes, errors.Wrapf(err, "compact %s", p) } changes = true - } - // If we didn't compact anything, there's nothing left to do. - if len(plans) == 0 { - break + runtime.GC() } }