Skip to content

Instantly share code, notes, and snippets.

@kgbu
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save kgbu/4eb7263908c0a2f6df83 to your computer and use it in GitHub Desktop.

Select an option

Save kgbu/4eb7263908c0a2f6df83 to your computer and use it in GitHub Desktop.
memory footprint for 200K goroutines and channels communications done in 18seconds

Environment

  • OS: Mac OSX Yosemite 10.10.3
  • Kernel (uname -prsv) : Darwin 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 i386
  • go version: go1.4.1 darwin/amd64
  • Machine: MacBook Air 13-inch Mid-2011
  • CPU: 1.8GHz Intel Core7 (2Core, Hyper Threading : 4 threads)
  • Memory: 4GB

Test code

https://github.com/kgbu/gorandom/commit/c10c0f30772659c00dd67809260cd19eea08bb73

package main

import (
	"log"
	"sync"
	"time"
)

func main() {
	var chans [200000]chan int
	var wg sync.WaitGroup

	for i := range chans {
   		chans[i] = make(chan int)
	}
	for i, ch := range chans {
		wg.Add(1)
		go func(i int, c chan int) {
			defer wg.Done()

			log.Printf("now receiving start %v, from chan %v", i, c)
			select {
			case cmd := <-c:
				log.Printf("OK: command received: %v", cmd)
			case <-time.After(100 * time.Second):
				log.Printf("FAIL: %v is timeout", i)
			}
			return
		}(i, ch)
	}
	log.Printf("now sending start")
	for i, ch := range chans {
		log.Printf("sending %v, %v",i, ch)
		ch <- i
	}
	wg.Wait()
	log.Printf("now all processes completed")
}

Execution

$ time go run manyproc.go 2> kk

real	0m18.899s
user	0m10.900s
sys	0m6.347s

$ grep FAIL kk | wc -l
       0

Memory footprint by Activity Monitor

  • Total Memory (required? virtual?) : 2.49GB
  • Memory after compression: 1.18GB
  • Threads: 4

In case of No LOG OUTPUT

$ time go run manyprocnolog.go 2> kkk

real	0m0.449s
user	0m0.132s
sys	0m0.053s

$ time go run manyprocnolog.go 2> kkk

real	0m0.159s
user	0m0.107s
sys	0m0.019s

$ grep FAIL kkk | wc -l
       0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment