blob: 56ef17249fde7c722c447d9077fc26138c3c1913 [file] [log] [blame]
Russ Coxd1bafff2011-10-13 15:54:23 -04001// $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
9package main
10
11import "runtime"
12
13var x []byte
14
15func init() {
16 c := make(chan int)
17 go send(c)
18 <-c
Rémy Oudompheng842c9062012-02-06 19:16:26 +010019
20 const chunk = 1 << 20
21 memstats := new(runtime.MemStats)
22 runtime.ReadMemStats(memstats)
23 sys := memstats.Sys
Russ Coxd1bafff2011-10-13 15:54:23 -040024 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 Oudompheng842c9062012-02-06 19:16:26 +010032 runtime.ReadMemStats(memstats)
33 sys1 := memstats.Sys
Russ Coxd1bafff2011-10-13 15:54:23 -040034 if sys1-sys > chunk*50 {
35 println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
36 }
37}
38
39func send(c chan int) {
40 c <- 1
41}
42
43func main() {
44}