2012-01-25 22:54:22 +01: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-27 01:57:58 +02:00
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGcSys(t *testing.T) {
|
2012-02-09 09:19:58 +01:00
|
|
|
memstats := new(runtime.MemStats)
|
2011-12-14 00:15:36 +01:00
|
|
|
runtime.GC()
|
2012-02-09 09:19:58 +01:00
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
sys := memstats.Sys
|
2011-12-14 00:15:36 +01:00
|
|
|
|
2012-04-20 20:40:14 +02:00
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
itercount := 1000000
|
|
|
|
if testing.Short() {
|
|
|
|
itercount = 100000
|
|
|
|
}
|
|
|
|
for i := 0; i < itercount; i++ {
|
2011-10-27 01:57:58 +02:00
|
|
|
workthegc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should only be using a few MB.
|
2012-02-09 09:19:58 +01:00
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
if sys > memstats.Sys {
|
2011-12-14 00:15:36 +01:00
|
|
|
sys = 0
|
|
|
|
} else {
|
2012-02-09 09:19:58 +01:00
|
|
|
sys = memstats.Sys - sys
|
2011-12-14 00:15:36 +01:00
|
|
|
}
|
|
|
|
t.Logf("used %d extra bytes", sys)
|
2011-12-14 15:54:32 +01:00
|
|
|
if sys > 4<<20 {
|
2011-12-14 00:15:36 +01:00
|
|
|
t.Fatalf("using too much memory: %d bytes", sys)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func workthegc() []byte {
|
|
|
|
return make([]byte, 1029)
|
|
|
|
}
|