Home | History | Annotate | Download | only in test
      1 // +build !nacl
      2 // run
      3 
      4 // Copyright 2011 The Go Authors. All rights reserved.
      5 // Use of this source code is governed by a BSD-style
      6 // license that can be found in the LICENSE file.
      7 
      8 // Test that buffered channels are garbage collected properly.
      9 // An interesting case because they have finalizers and used to
     10 // have self loops that kept them from being collected.
     11 // (Cyclic data with finalizers is never finalized, nor collected.)
     12 
     13 package main
     14 
     15 import (
     16 	"fmt"
     17 	"os"
     18 	"runtime"
     19 )
     20 
     21 func main() {
     22 	const N = 10000
     23 	st := new(runtime.MemStats)
     24 	memstats := new(runtime.MemStats)
     25 	runtime.ReadMemStats(st)
     26 	for i := 0; i < N; i++ {
     27 		c := make(chan int, 10)
     28 		_ = c
     29 		if i%100 == 0 {
     30 			for j := 0; j < 4; j++ {
     31 				runtime.GC()
     32 				runtime.Gosched()
     33 				runtime.GC()
     34 				runtime.Gosched()
     35 			}
     36 		}
     37 	}
     38 
     39 	runtime.ReadMemStats(memstats)
     40 	obj := int64(memstats.HeapObjects - st.HeapObjects)
     41 	if obj > N/5 {
     42 		fmt.Println("too many objects left:", obj)
     43 		os.Exit(1)
     44 	}
     45 }
     46