2012-02-16 21:50:37 -07:00
|
|
|
// run
|
2011-10-13 13:54:23 -06:00
|
|
|
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Test that goroutines and garbage collection run during init.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "runtime"
|
|
|
|
|
|
|
|
var x []byte
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
c := make(chan int)
|
|
|
|
go send(c)
|
|
|
|
<-c
|
2012-02-06 11:16:26 -07:00
|
|
|
|
|
|
|
const chunk = 1 << 20
|
|
|
|
memstats := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
sys := memstats.Sys
|
2011-10-13 13:54:23 -06:00
|
|
|
b := make([]byte, chunk)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i%10 + '0')
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
x = []byte(s)
|
|
|
|
}
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
sys1 := memstats.Sys
|
2015-01-06 12:58:49 -07:00
|
|
|
if sys1-sys > chunk*500 {
|
2011-10-13 13:54:23 -06:00
|
|
|
println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
|
2013-02-12 11:17:49 -07:00
|
|
|
panic("init1")
|
2011-10-13 13:54:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func send(c chan int) {
|
|
|
|
c <- 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
}
|