From 8219cc9af8a53fa22af2e12631108c85a14b166b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 12 Oct 2011 13:23:34 -0400 Subject: [PATCH] runtime: fix memory leak in parallel garbage collector The work buffer management used by the garbage collector during parallel collections leaks buffers. This CL tests for and fixes the leak. R=golang-dev, dvyukov, r CC=golang-dev https://golang.org/cl/5254059 --- src/pkg/runtime/gc_test.go | 24 ++++++++++++++++++++++++ src/pkg/runtime/mgc0.c | 2 +- src/run.bash | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/pkg/runtime/gc_test.go diff --git a/src/pkg/runtime/gc_test.go b/src/pkg/runtime/gc_test.go new file mode 100644 index 0000000000..fad60a3680 --- /dev/null +++ b/src/pkg/runtime/gc_test.go @@ -0,0 +1,24 @@ +package runtime_test + +import ( + "runtime" + "testing" +) + +func TestGcSys(t *testing.T) { + for i := 0; i < 1000000; i++ { + workthegc() + } + + // Should only be using a few MB. + runtime.UpdateMemStats() + sys := runtime.MemStats.Sys + t.Logf("using %d MB", sys>>20) + if sys > 10e6 { + t.Fatalf("using too much memory: %d MB", sys>>20) + } +} + +func workthegc() []byte { + return make([]byte, 1029) +} diff --git a/src/pkg/runtime/mgc0.c b/src/pkg/runtime/mgc0.c index 6f7e2459d9..89bad668fe 100644 --- a/src/pkg/runtime/mgc0.c +++ b/src/pkg/runtime/mgc0.c @@ -501,7 +501,7 @@ putempty(Workbuf *b) runtimeĀ·lock(&work.emu); b->next = work.empty; - work.empty = b->next; + work.empty = b; runtimeĀ·unlock(&work.emu); } diff --git a/src/run.bash b/src/run.bash index d3d2c69bf5..927e193e0a 100755 --- a/src/run.bash +++ b/src/run.bash @@ -41,6 +41,10 @@ fi gomake testshort ) || exit $? +(xcd pkg/runtime; +gotest -short -cpu=1,2,4 +) || exit $? + (xcd pkg/sync; GOMAXPROCS=10 gomake testshort ) || exit $?