2008-12-18 16:42:28 -07:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
// Repeated malloc test.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-02-03 17:31:34 -07:00
|
|
|
"flag"
|
|
|
|
"runtime"
|
2008-12-18 16:42:28 -07:00
|
|
|
)
|
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
var chatty = flag.Bool("v", false, "chatty")
|
|
|
|
|
|
|
|
var oldsys uint64
|
2012-02-06 11:16:26 -07:00
|
|
|
var memstats runtime.MemStats
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
func bigger() {
|
2012-02-06 11:16:26 -07:00
|
|
|
st := &memstats
|
|
|
|
runtime.ReadMemStats(st)
|
|
|
|
if oldsys < st.Sys {
|
2010-02-03 17:31:34 -07:00
|
|
|
oldsys = st.Sys
|
2009-01-09 14:42:46 -07:00
|
|
|
if *chatty {
|
2010-02-03 17:31:34 -07:00
|
|
|
println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2009-01-16 17:12:14 -07:00
|
|
|
if st.Sys > 1e9 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("too big")
|
|
|
|
panic("fail")
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.GC() // clean up garbage from init
|
|
|
|
runtime.ReadMemStats(&memstats) // first call can do some allocations
|
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
|
|
|
stacks := memstats.Alloc // ignore stacks
|
2010-03-24 10:40:09 -06:00
|
|
|
flag.Parse()
|
2009-10-09 12:18:32 -06:00
|
|
|
for i := 0; i < 1<<7; i++ {
|
2010-02-03 17:31:34 -07:00
|
|
|
for j := 1; j <= 1<<22; j <<= 1 {
|
2009-01-09 14:42:46 -07:00
|
|
|
if i == 0 && *chatty {
|
2010-02-03 17:31:34 -07:00
|
|
|
println("First alloc:", j)
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2012-02-06 11:16:26 -07:00
|
|
|
if a := memstats.Alloc - stacks; a != 0 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("no allocations but stats report", a, "bytes allocated")
|
|
|
|
panic("fail")
|
2009-06-05 11:59:37 -06:00
|
|
|
}
|
2010-02-03 17:31:34 -07:00
|
|
|
b := runtime.Alloc(uintptr(j))
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(&memstats)
|
|
|
|
during := memstats.Alloc - stacks
|
2010-02-03 17:31:34 -07:00
|
|
|
runtime.Free(b)
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(&memstats)
|
|
|
|
if a := memstats.Alloc - stacks; a != 0 {
|
2010-03-30 11:34:57 -06:00
|
|
|
println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
|
|
|
|
panic("fail")
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2010-02-03 17:31:34 -07:00
|
|
|
bigger()
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2009-01-09 14:42:46 -07:00
|
|
|
if i%(1<<10) == 0 && *chatty {
|
2010-02-03 17:31:34 -07:00
|
|
|
println(i)
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
if i == 0 {
|
2009-01-09 14:42:46 -07:00
|
|
|
if *chatty {
|
2010-02-03 17:31:34 -07:00
|
|
|
println("Primed", i)
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
2010-09-03 18:36:13 -06:00
|
|
|
// runtime.frozen = true
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|