Last active
December 25, 2015 02:09
-
-
Save alco/6900869 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 ( | |
"time" | |
"fmt" | |
"runtime" | |
) | |
const NROUTINES = 1000000 | |
func countThis(c chan int) { | |
c <- 1 | |
} | |
func main() { | |
count_chan := make(chan int) | |
spawnTime := time.Now() | |
for i := 0; i < NROUTINES; i++ { | |
go countThis(count_chan) | |
} | |
spawnDelta := time.Now().Sub(spawnTime) | |
var m runtime.MemStats | |
runtime.ReadMemStats(&m) | |
fmt.Printf("Used memory after creating %v blocked goroutines:\n\tAllocs: %v bytes\n\tHeapAlloc: %v bytes\n\tSys mem: %v bytes\n\tNumGC: %v\n\n", NROUTINES, m.Alloc, m.HeapAlloc, m.Sys, m.NumGC) | |
readTime := time.Now() | |
total := 0 | |
for i := 0; i < NROUTINES; i++ { | |
total += <-count_chan | |
} | |
close(count_chan) | |
readDelta := time.Now().Sub(readTime) | |
fmt.Printf("After running %v goroutines got %v as result\n", NROUTINES, total) | |
fmt.Printf("Spawn time: %v; read from channel time: %v\n\n", spawnDelta, readDelta) | |
fmt.Printf("Remaining goroutines: %v\n", runtime.NumGoroutine()) | |
time.Sleep(100 * time.Millisecond) | |
fmt.Printf("Remaining goroutines after sleep: %v\n", runtime.NumGoroutine()) | |
runtime.GC() | |
runtime.ReadMemStats(&m) | |
fmt.Printf("Used memory after freeing %v blocked goroutines:\n\tAllocs: %v bytes\n\tHeapAlloc: %v bytes\n\tSys mem: %v bytes\n\tNumGC: %v\n\n", NROUTINES, m.Alloc, m.HeapAlloc, m.Sys, m.NumGC) | |
} |
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
λ go run main.go | |
Used memory after creating 1000000 blocked goroutines: | |
Allocs: 264672024 bytes | |
HeapAlloc: 264672024 bytes | |
Sys mem: 4715580600 bytes | |
NumGC: 2 | |
After running 1000000 goroutines got 1000000 as result | |
Remaining goroutines: 1000001 | |
Remaining goroutines after sleep: 3 | |
Used memory after freeing 1000000 blocked goroutines: | |
Allocs: 264724712 bytes | |
HeapAlloc: 264724712 bytes | |
Sys mem: 4715580600 bytes | |
NumGC: 3 | |
λ go version | |
go version go1.1.2 darwin/amd64 |
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
λ /Users/alco/Documents/git/go/bin/go run main.go | |
Used memory after creating 1000000 blocked goroutines: | |
Allocs: 296576264 bytes | |
HeapAlloc: 296576264 bytes | |
Sys mem: 8583142648 bytes | |
NumGC: 3 | |
After running 1000000 goroutines got 1000000 as result | |
Remaining goroutines: 873972 | |
Remaining goroutines after sleep: 3 | |
Used memory after freeing 1000000 blocked goroutines: | |
Allocs: 296576664 bytes | |
HeapAlloc: 296576664 bytes | |
Sys mem: 8584191224 bytes | |
NumGC: 4 | |
λ /Users/alco/Documents/git/go/bin/go version | |
go version devel +7bc58b0d9de0 Wed Oct 09 08:37:06 2013 -0400 darwin/amd64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment