github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/test/fixedbugs/issue15277.go (about)

     1  // run
     2  
     3  // Copyright 2016 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  // +build amd64
     7  
     8  package main
     9  
    10  import "runtime"
    11  
    12  type big [10 << 20]byte
    13  
    14  func f(x *big, start int64) {
    15  	if delta := inuse() - start; delta < 9<<20 {
    16  		println("after alloc: expected delta at least 9MB, got: ", delta)
    17  	}
    18  	x = nil
    19  	if delta := inuse() - start; delta > 1<<20 {
    20  		println("after drop: expected delta below 1MB, got: ", delta)
    21  	}
    22  	x = new(big)
    23  	if delta := inuse() - start; delta < 9<<20 {
    24  		println("second alloc: expected delta at least 9MB, got: ", delta)
    25  	}
    26  }
    27  
    28  func main() {
    29  	x := inuse()
    30  	f(new(big), x)
    31  }
    32  
    33  func inuse() int64 {
    34  	runtime.GC()
    35  	var st runtime.MemStats
    36  	runtime.ReadMemStats(&st)
    37  	return int64(st.Alloc)
    38  }