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