Advertisement
Guest User

go leak

a guest
Nov 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.69 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "runtime"
  7.     "time"
  8. )
  9.  
  10. var routines = 800
  11.  
  12. func main() {
  13.     printTime := func(format string, i ...interface{}) {
  14.         fmt.Printf(fmt.Sprintf("[%s] %s", time.Now().Format(time.StampMilli), format), i...)
  15.     }
  16.  
  17.     i := 0
  18.  
  19.     go func() {
  20.         for {
  21.             printTime("goroutines = %d; i = %d\n", runtime.NumGoroutine(), i)
  22.             time.Sleep(time.Second * 2)
  23.         }
  24.     }()
  25.  
  26.     time.Sleep(time.Second * 5)
  27.  
  28.     printTime("create %d routines\n", routines)
  29.  
  30.     for ; i < routines; i++ {
  31.         go func() {
  32.             time.Sleep(time.Second * 5)
  33.             i--
  34.         }()
  35.     }
  36.  
  37.     for i > 0 {
  38.         time.Sleep(time.Second)
  39.     }
  40.  
  41.     printTime("end of routines\n")
  42.     time.Sleep(time.Second * 20)
  43.     os.Exit(0)
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement