blob: 43233b7b74b042234704c862bc4d2afbc5bb19aa [file] [log] [blame]
Russ Coxe29ce172008-12-18 15:42:28 -08001// $G $D/$F.go && $L $F.$A && ./$A.out
2
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Repeated malloc test.
8
9package main
10
11import (
Russ Cox33e396a2010-02-03 16:31:34 -080012 "flag"
13 "runtime"
Russ Coxe29ce172008-12-18 15:42:28 -080014)
15
Russ Cox33e396a2010-02-03 16:31:34 -080016var chatty = flag.Bool("v", false, "chatty")
Russ Coxe29ce172008-12-18 15:42:28 -080017
Russ Cox33e396a2010-02-03 16:31:34 -080018var oldsys uint64
19
Russ Coxe29ce172008-12-18 15:42:28 -080020func bigger() {
Russ Cox33e396a2010-02-03 16:31:34 -080021 if st := runtime.MemStats; oldsys < st.Sys {
22 oldsys = st.Sys
Rob Pikec45d2a72009-01-09 13:42:46 -080023 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080024 println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
Russ Coxe29ce172008-12-18 15:42:28 -080025 }
Russ Coxf48cbfd2009-01-16 16:12:14 -080026 if st.Sys > 1e9 {
Rob Pike325cf8e2010-03-24 16:46:53 -070027 println("too big")
28 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080029 }
30 }
31}
32
33func main() {
Ian Lance Taylor9d93d572010-09-10 15:54:16 -070034 runtime.GC() // clean up garbage from init
Russ Cox6eb251f2010-03-24 09:40:09 -070035 runtime.MemProfileRate = 0 // disable profiler
Russ Cox33e396a2010-02-03 16:31:34 -080036 runtime.MemStats.Alloc = 0 // ignore stacks
Russ Cox6eb251f2010-03-24 09:40:09 -070037 flag.Parse()
Russ Coxebd27d62009-10-09 11:18:32 -070038 for i := 0; i < 1<<7; i++ {
Russ Cox33e396a2010-02-03 16:31:34 -080039 for j := 1; j <= 1<<22; j <<= 1 {
Rob Pikec45d2a72009-01-09 13:42:46 -080040 if i == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080041 println("First alloc:", j)
Russ Coxe29ce172008-12-18 15:42:28 -080042 }
Russ Cox33e396a2010-02-03 16:31:34 -080043 if a := runtime.MemStats.Alloc; a != 0 {
Rob Pike325cf8e2010-03-24 16:46:53 -070044 println("no allocations but stats report", a, "bytes allocated")
45 panic("fail")
Russ Coxb014be72009-06-05 10:59:37 -070046 }
Russ Cox33e396a2010-02-03 16:31:34 -080047 b := runtime.Alloc(uintptr(j))
48 during := runtime.MemStats.Alloc
49 runtime.Free(b)
Dmitriy Vyukov66d5c9b2011-07-18 14:52:57 -040050 runtime.GC()
Russ Cox33e396a2010-02-03 16:31:34 -080051 if a := runtime.MemStats.Alloc; a != 0 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070052 println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
53 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080054 }
Russ Cox33e396a2010-02-03 16:31:34 -080055 bigger()
Russ Coxe29ce172008-12-18 15:42:28 -080056 }
Rob Pikec45d2a72009-01-09 13:42:46 -080057 if i%(1<<10) == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080058 println(i)
Russ Coxe29ce172008-12-18 15:42:28 -080059 }
60 if i == 0 {
Rob Pikec45d2a72009-01-09 13:42:46 -080061 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080062 println("Primed", i)
Russ Coxe29ce172008-12-18 15:42:28 -080063 }
Rob Pike4f61fc92010-09-04 10:36:13 +100064 // runtime.frozen = true
Russ Coxe29ce172008-12-18 15:42:28 -080065 }
66 }
67}