package main import ( "fmt" "runtime" "time" ) type buffer struct { a int b []byte c int } func main() { c := make(chan bool) const n = 500 for i := 0; i < n; i++ { go func() { myc := c for { var b buffer <-myc // pass by here once, then block forever. myc = nil // This buffer should only survive for the next 2 lines // and the call to foo. Once the for loop returns to // the "var b buffer" line, b.b should be zero, and the // GC should have collected it. b.b = make([]byte, 2 << 20) foo(b) // This doesn't even help: // b.b = nil } }() } for i := 0; i < n; i ++ { select { case c <- true: default: } runtime.GC() var s runtime.MemStats runtime.ReadMemStats(&s) fmt.Printf("alloc (still in use): %d\n", s.Alloc) fmt.Printf(" sys: %d\n", s.Sys) fmt.Printf(" num gc: %d\n", s.NumGC) fmt.Println() time.Sleep(1 * time.Second) } } func foo(b buffer) { // nothing }