1// run 2 3//go:build amd64 4 5// Copyright 2016 The Go Authors. All rights reserved. 6// Use of this source code is governed by a BSD-style 7// license that can be found in the LICENSE file. 8 9package main 10 11import "runtime" 12 13type big [10 << 20]byte 14 15func f(x *big, start int64) { 16 if delta := inuse() - start; delta < 9<<20 { 17 println("after alloc: expected delta at least 9MB, got: ", delta) 18 } 19 runtime.KeepAlive(x) 20 x = nil 21 if delta := inuse() - start; delta > 1<<20 { 22 println("after drop: expected delta below 1MB, got: ", delta) 23 } 24 x = new(big) 25 if delta := inuse() - start; delta < 9<<20 { 26 println("second alloc: expected delta at least 9MB, got: ", delta) 27 } 28 runtime.KeepAlive(x) 29} 30 31func main() { 32 x := inuse() 33 f(new(big), x) 34} 35 36func inuse() int64 { 37 runtime.GC() 38 var st runtime.MemStats 39 runtime.ReadMemStats(&st) 40 return int64(st.Alloc) 41} 42