Russ Cox | d1bafff | 2011-10-13 15:54:23 -0400 | [diff] [blame] | 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 | // Test that goroutines and garbage collection run during init. |
| 8 | |
| 9 | package main |
| 10 | |
| 11 | import "runtime" |
| 12 | |
| 13 | var x []byte |
| 14 | |
| 15 | func init() { |
| 16 | c := make(chan int) |
| 17 | go send(c) |
| 18 | <-c |
Rémy Oudompheng | 842c906 | 2012-02-06 19:16:26 +0100 | [diff] [blame^] | 19 | |
| 20 | const chunk = 1 << 20 |
| 21 | memstats := new(runtime.MemStats) |
| 22 | runtime.ReadMemStats(memstats) |
| 23 | sys := memstats.Sys |
Russ Cox | d1bafff | 2011-10-13 15:54:23 -0400 | [diff] [blame] | 24 | b := make([]byte, chunk) |
| 25 | for i := range b { |
| 26 | b[i] = byte(i%10 + '0') |
| 27 | } |
| 28 | s := string(b) |
| 29 | for i := 0; i < 1000; i++ { |
| 30 | x = []byte(s) |
| 31 | } |
Rémy Oudompheng | 842c906 | 2012-02-06 19:16:26 +0100 | [diff] [blame^] | 32 | runtime.ReadMemStats(memstats) |
| 33 | sys1 := memstats.Sys |
Russ Cox | d1bafff | 2011-10-13 15:54:23 -0400 | [diff] [blame] | 34 | if sys1-sys > chunk*50 { |
| 35 | println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func send(c chan int) { |
| 40 | c <- 1 |
| 41 | } |
| 42 | |
| 43 | func main() { |
| 44 | } |