1
0
mirror of https://github.com/golang/go synced 2024-11-12 04:40:22 -07:00
go/test/mallocrep.go
Rob Pike 325cf8ef21 delete all uses of panicln by rewriting them using panic or,
in the tests, println+panic.
gofmt some tests too.

R=rsc
CC=golang-dev
https://golang.org/cl/741041
2010-03-24 16:46:53 -07:00

65 lines
1.3 KiB
Go

// $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 (
"flag"
"runtime"
)
var chatty = flag.Bool("v", false, "chatty")
var oldsys uint64
func bigger() {
if st := runtime.MemStats; oldsys < st.Sys {
oldsys = st.Sys
if *chatty {
println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
}
if st.Sys > 1e9 {
println("too big")
panic("fail")
}
}
}
func main() {
runtime.MemProfileRate = 0 // disable profiler
runtime.MemStats.Alloc = 0 // ignore stacks
flag.Parse()
for i := 0; i < 1<<7; i++ {
for j := 1; j <= 1<<22; j <<= 1 {
if i == 0 && *chatty {
println("First alloc:", j)
}
if a := runtime.MemStats.Alloc; a != 0 {
println("no allocations but stats report", a, "bytes allocated")
panic("fail")
}
b := runtime.Alloc(uintptr(j))
during := runtime.MemStats.Alloc
runtime.Free(b)
if a := runtime.MemStats.Alloc; a != 0 {
panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
}
bigger()
}
if i%(1<<10) == 0 && *chatty {
println(i)
}
if i == 0 {
if *chatty {
println("Primed", i)
}
// runtime.frozen = true;
}
}
}