mirror of
https://github.com/golang/go
synced 2024-11-24 01:40:12 -07:00
db7fd1c142
run GC in its own background goroutine making the caller runnable if resources are available. This is critical in single goroutine applications. Allow goroutines that allocate a lot to help out the GC and in doing so throttle their own allocation. Adjust test so that it only detects that a GC is run during init calls and not whether the GC is memory efficient. Memory efficiency work will happen later in 1.5. Change-Id: I4306f5e377bb47c69bda1aedba66164f12b20c2b Reviewed-on: https://go-review.googlesource.com/2349 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com>
46 lines
816 B
Go
46 lines
816 B
Go
// run
|
|
|
|
// 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
|
|
|
|
const chunk = 1 << 20
|
|
memstats := new(runtime.MemStats)
|
|
runtime.ReadMemStats(memstats)
|
|
sys := memstats.Sys
|
|
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)
|
|
}
|
|
runtime.ReadMemStats(memstats)
|
|
sys1 := memstats.Sys
|
|
if sys1-sys > chunk*500 {
|
|
println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
|
|
panic("init1")
|
|
}
|
|
}
|
|
|
|
func send(c chan int) {
|
|
c <- 1
|
|
}
|
|
|
|
func main() {
|
|
}
|