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
|
2008-12-18 16:42:28 -07:00
|
|
|
|
|
|
|
func bigger() {
|
2010-02-03 17:31:34 -07:00
|
|
|
if st := runtime.MemStats; oldsys < st.Sys {
|
|
|
|
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() {
|
2010-03-24 10:40:09 -06:00
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
2010-02-03 17:31:34 -07:00
|
|
|
runtime.MemStats.Alloc = 0 // 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
|
|
|
}
|
2010-02-03 17:31:34 -07:00
|
|
|
if a := runtime.MemStats.Alloc; 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))
|
|
|
|
during := runtime.MemStats.Alloc
|
|
|
|
runtime.Free(b)
|
|
|
|
if a := runtime.MemStats.Alloc; 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-02-03 17:31:34 -07:00
|
|
|
// runtime.frozen = true;
|
2008-12-18 16:42:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|