-
-
Save srosenberg/a4b920560f61c1f8a41869dfaed051bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func tickerLoop(wg *sync.WaitGroup, quit chan struct{}) { | |
t := time.NewTicker(time.Duration(10+rand.Intn(20)) * time.Millisecond) | |
defer wg.Done() | |
// NB: uncomment to fix a memory leak | |
defer t.Stop() | |
select { | |
case <-t.C: | |
case <-quit: | |
return | |
} | |
} | |
func createTickers(n int, quit chan struct{}) { | |
wg := new(sync.WaitGroup) | |
wg.Add(n) | |
fmt.Printf("Making %d tickers\n", n) | |
for i := 0; i < n; i++ { | |
go tickerLoop(wg, quit) | |
} | |
// NB: quit will exit out of the goroutine, effectively allowing to GC ticker's channel. | |
// However, the global 'timers' array continues to point to ticker's 'runtimeTimer' struct | |
// unless `Stop` is called. | |
close(quit) | |
wg.Wait() | |
} | |
func main() { | |
const n = 100000 | |
quit := make(chan struct{}) | |
var start, end runtime.MemStats | |
runtime.GC() | |
runtime.ReadMemStats(&start) | |
createTickers(n, quit) | |
// Run a dummy goroutine so that select {} doesn't throw a tantrum. | |
// It panics otherwise -- fatal error: all goroutines are asleep - deadlock! | |
i := 0 | |
go func() { | |
for { | |
i += 1 | |
time.Sleep(1 * time.Millisecond) | |
} | |
}() | |
time.Sleep(2 * time.Second) | |
runtime.GC() | |
runtime.ReadMemStats(&end) | |
alloc := end.TotalAlloc - start.TotalAlloc | |
fmt.Printf("Total heap: %d\n", alloc) | |
fmt.Printf("Sleeping... check CPU utilization\n") | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment