modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue15281.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 package main 7 8 import "runtime" 9 10 func main() { 11 { 12 x := inuse() 13 c := make(chan []byte, 10) 14 c <- make([]byte, 10<<20) 15 close(c) 16 f1(c, x) 17 } 18 { 19 x := inuse() 20 c := make(chan []byte, 10) 21 c <- make([]byte, 10<<20) 22 close(c) 23 f2(c, x) 24 } 25 } 26 27 func f1(c chan []byte, start int64) { 28 for x := range c { 29 if delta := inuse() - start; delta < 9<<20 { 30 println("BUG: f1: after alloc: expected delta at least 9MB, got: ", delta) 31 println(x) 32 } 33 x = nil 34 if delta := inuse() - start; delta > 1<<20 { 35 println("BUG: f1: after alloc: expected delta below 1MB, got: ", delta) 36 println(x) 37 } 38 } 39 } 40 41 func f2(c chan []byte, start int64) { 42 for { 43 x, ok := <-c 44 if !ok { 45 break 46 } 47 if delta := inuse() - start; delta < 9<<20 { 48 println("BUG: f2: after alloc: expected delta at least 9MB, got: ", delta) 49 println(x) 50 } 51 x = nil 52 if delta := inuse() - start; delta > 1<<20 { 53 println("BUG: f2: after alloc: expected delta below 1MB, got: ", delta) 54 println(x) 55 } 56 } 57 } 58 59 func inuse() int64 { 60 runtime.GC() 61 var st runtime.MemStats 62 runtime.ReadMemStats(&st) 63 return int64(st.Alloc) 64 }