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-12-01 00:38:01 +08:00
|
|
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
|
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-04-20 11:36:06 -07:00
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
|
|
|
|
|
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-05-22 00:07:13 -04:00
|
|
|
// We allocated 100 MB or (if not short) 1 GB.
|
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)
|
2012-05-22 00:07:13 -04:00
|
|
|
if sys > 16<<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)
|
|
|
|
|
}
|
2012-09-12 12:08:27 -04:00
|
|
|
|
|
|
|
|
func TestGcDeepNesting(t *testing.T) {
|
|
|
|
|
type T [2][2][2][2][2][2][2][2][2][2]*int
|
|
|
|
|
a := new(T)
|
|
|
|
|
|
|
|
|
|
// Prevent the compiler from applying escape analysis.
|
|
|
|
|
// This makes sure new(T) is allocated on heap, not on the stack.
|
|
|
|
|
t.Logf("%p", a)
|
|
|
|
|
|
|
|
|
|
a[0][0][0][0][0][0][0][0][0][0] = new(int)
|
|
|
|
|
*a[0][0][0][0][0][0][0][0][0][0] = 13
|
|
|
|
|
runtime.GC()
|
|
|
|
|
if *a[0][0][0][0][0][0][0][0][0][0] != 13 {
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
}
|