2012-02-16 21:48:57 -07:00
|
|
|
// run
|
2010-04-01 12:56:18 -06:00
|
|
|
|
2016-04-10 15:32:26 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-04-01 12:56:18 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-18 23:44:02 -07:00
|
|
|
// Test that selects do not consume undue memory.
|
|
|
|
|
2010-04-01 12:56:18 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "runtime"
|
|
|
|
|
|
|
|
func sender(c chan int, n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
c <- 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func receiver(c, dummy chan int, n int) {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
// nothing
|
|
|
|
case <-dummy:
|
|
|
|
panic("dummy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
runtime.MemProfileRate = 0
|
|
|
|
|
|
|
|
c := make(chan int)
|
|
|
|
dummy := make(chan int)
|
|
|
|
|
|
|
|
// warm up
|
|
|
|
go sender(c, 100000)
|
|
|
|
receiver(c, dummy, 100000)
|
|
|
|
runtime.GC()
|
2012-02-06 11:16:26 -07:00
|
|
|
memstats := new(runtime.MemStats)
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
alloc := memstats.Alloc
|
2010-04-01 12:56:18 -06:00
|
|
|
|
|
|
|
// second time shouldn't increase footprint by much
|
|
|
|
go sender(c, 100000)
|
|
|
|
receiver(c, dummy, 100000)
|
|
|
|
runtime.GC()
|
2012-02-06 11:16:26 -07:00
|
|
|
runtime.ReadMemStats(memstats)
|
2010-04-01 12:56:18 -06:00
|
|
|
|
2013-09-20 18:27:56 -06:00
|
|
|
// Be careful to avoid wraparound.
|
|
|
|
if memstats.Alloc > alloc && memstats.Alloc-alloc > 1.1e5 {
|
2012-02-06 11:16:26 -07:00
|
|
|
println("BUG: too much memory for 100,000 selects:", memstats.Alloc-alloc)
|
2010-04-01 12:56:18 -06:00
|
|
|
}
|
|
|
|
}
|