2012-01-19 11:14:56 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-10-12 11:23:34 -06:00
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGcSys(t *testing.T) {
|
2012-02-06 11:16:26 -07:00
|
|
|
memstats := new(runtime.MemStats)
|
2011-12-13 16:12:55 -07:00
|
|
|
runtime.GC()
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
sys := memstats.Sys
|
2011-12-13 16:12:55 -07:00
|
|
|
|
2012-04-20 12:36:06 -06:00
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
|
|
|
|
2012-02-14 14:13:19 -07:00
|
|
|
itercount := 1000000
|
|
|
|
if testing.Short() {
|
|
|
|
itercount = 100000
|
|
|
|
}
|
|
|
|
for i := 0; i < itercount; i++ {
|
2011-10-12 11:23:34 -06:00
|
|
|
workthegc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should only be using a few MB.
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
if sys > memstats.Sys {
|
2011-12-13 16:12:55 -07:00
|
|
|
sys = 0
|
|
|
|
} else {
|
2012-02-06 11:16:26 -07:00
|
|
|
sys = memstats.Sys - sys
|
2011-12-13 16:12:55 -07:00
|
|
|
}
|
|
|
|
t.Logf("used %d extra bytes", sys)
|
2011-12-13 22:28:43 -07:00
|
|
|
if sys > 4<<20 {
|
2011-12-13 16:12:55 -07:00
|
|
|
t.Fatalf("using too much memory: %d bytes", sys)
|
2011-10-12 11:23:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func workthegc() []byte {
|
|
|
|
return make([]byte, 1029)
|
|
|
|
}
|