Richard Musiol | e3c6847 | 2018-03-04 12:15:37 +0100 | [diff] [blame] | 1 | // +build !nacl,!js |
Rahul Chaudhry | 8581d48 | 2015-02-03 10:52:18 -0800 | [diff] [blame] | 2 | // run |
Russ Cox | 0c2a727 | 2014-05-20 12:10:19 -0400 | [diff] [blame] | 3 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 4 | // Copyright 2011 The Go Authors. All rights reserved. |
Russ Cox | 507df95 | 2011-04-13 23:42:06 -0400 | [diff] [blame] | 5 | // Use of this source code is governed by a BSD-style |
| 6 | // license that can be found in the LICENSE file. |
| 7 | |
Rob Pike | 83976e3 | 2012-02-19 14:28:53 +1100 | [diff] [blame] | 8 | // Test that buffered channels are garbage collected properly. |
Russ Cox | 507df95 | 2011-04-13 23:42:06 -0400 | [diff] [blame] | 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 |
Rémy Oudompheng | 842c906 | 2012-02-06 19:16:26 +0100 | [diff] [blame] | 23 | st := new(runtime.MemStats) |
| 24 | memstats := new(runtime.MemStats) |
| 25 | runtime.ReadMemStats(st) |
Russ Cox | 507df95 | 2011-04-13 23:42:06 -0400 | [diff] [blame] | 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 | } |
Russ Cox | 226fb09 | 2011-07-22 00:55:01 -0400 | [diff] [blame] | 38 | |
Rémy Oudompheng | 842c906 | 2012-02-06 19:16:26 +0100 | [diff] [blame] | 39 | runtime.ReadMemStats(memstats) |
Russ Cox | 0c2a727 | 2014-05-20 12:10:19 -0400 | [diff] [blame] | 40 | obj := int64(memstats.HeapObjects - st.HeapObjects) |
Russ Cox | 507df95 | 2011-04-13 23:42:06 -0400 | [diff] [blame] | 41 | if obj > N/5 { |
| 42 | fmt.Println("too many objects left:", obj) |
| 43 | os.Exit(1) |
| 44 | } |
| 45 | } |