]> Cypherpunks.ru repositories - gostls13.git/blob - test/gc2.go
runtime: drop chan circular linked list in favor of circular buffer
[gostls13.git] / test / gc2.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2011 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
7 // Check that buffered channels are garbage collected properly.
8 // An interesting case because they have finalizers and used to
9 // have self loops that kept them from being collected.
10 // (Cyclic data with finalizers is never finalized, nor collected.)
11
12 package main
13
14 import (
15         "fmt"
16         "os"
17         "runtime"
18 )
19
20 func main() {
21         const N = 10000
22         st := runtime.MemStats
23         for i := 0; i < N; i++ {
24                 c := make(chan int, 10)
25                 _ = c
26                 if i%100 == 0 {
27                         for j := 0; j < 4; j++ {
28                                 runtime.GC()
29                                 runtime.Gosched()
30                                 runtime.GC()
31                                 runtime.Gosched()
32                         }
33                 }
34         }
35         
36         obj := runtime.MemStats.HeapObjects - st.HeapObjects
37         if obj > N/5 {
38                 fmt.Println("too many objects left:", obj)
39                 os.Exit(1)
40         }
41 }