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