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

Periodically run GC in all dgraph commands. #4032

Merged
merged 3 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions dgraph/cmd/bulk/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import (
"runtime"
"strconv"
"strings"
"time"

"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/x"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -204,27 +202,6 @@ func run() {
defer os.RemoveAll(opt.TmpDir)
}

// Bulk loader can take up a lot of RAM. So, run GC often.
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()

var lastNum uint32
var ms runtime.MemStats
for range ticker.C {
runtime.ReadMemStats(&ms)
fmt.Printf("GC: %d. InUse: %s. Idle: %s\n", ms.NumGC, humanize.Bytes(ms.HeapInuse),
humanize.Bytes(ms.HeapIdle-ms.HeapReleased))
if ms.NumGC > lastNum {
// GC was already run by the Go runtime. No need to run it again.
lastNum = ms.NumGC
} else {
runtime.GC()
lastNum = ms.NumGC + 1
}
}
}()

loader := newLoader(opt)
if !opt.SkipMapPhase {
loader.mapStage()
Expand Down
25 changes: 25 additions & 0 deletions dgraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"time"

"github.com/dgraph-io/dgraph/dgraph/cmd"
"github.com/dustin/go-humanize"
"github.com/golang/glog"
)

func main() {
Expand All @@ -30,5 +32,28 @@ func main() {
// improving throughput. The extra CPU overhead is almost negligible in comparison. The
// benchmark notes are located in badger-bench/randread.
runtime.GOMAXPROCS(128)

// Make sure the garbage collector is run periodically.
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()

var lastNum uint32
var ms runtime.MemStats
for range ticker.C {
runtime.ReadMemStats(&ms)
if ms.NumGC > lastNum {
// GC was already run by the Go runtime. No need to run it again.
lastNum = ms.NumGC
} else {
runtime.GC()
glog.V(2).Infof("GC: %d. InUse: %s. Idle: %s\n", ms.NumGC,
humanize.Bytes(ms.HeapInuse),
humanize.Bytes(ms.HeapIdle-ms.HeapReleased))
lastNum = ms.NumGC + 1
}
}
}()

cmd.Execute()
}