modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/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  	runtime.KeepAlive(x)
    19  	x = nil
    20  	if delta := inuse() - start; delta > 1<<20 {
    21  		println("after drop: expected delta below 1MB, got: ", delta)
    22  	}
    23  	x = new(big)
    24  	if delta := inuse() - start; delta < 9<<20 {
    25  		println("second alloc: expected delta at least 9MB, got: ", delta)
    26  	}
    27  	runtime.KeepAlive(x)
    28  }
    29  
    30  func main() {
    31  	x := inuse()
    32  	f(new(big), x)
    33  }
    34  
    35  func inuse() int64 {
    36  	runtime.GC()
    37  	var st runtime.MemStats
    38  	runtime.ReadMemStats(&st)
    39  	return int64(st.Alloc)
    40  }