title: Golang goroutine memory leak
body: <p>I can't understand why memory is not freed. Memory profile shows that almost all of the memory used by <strong>runtime.malg</strong>. If I remove range over channel in DoSomeWork everything works fine.</p>
  
  <p>Here of the output of fmt.Println:</p>
  
  <pre><code>Memory Acquired:  4196600
  Memory Used    :  745192
  
  Memory Acquired:  2651299576
  Memory Used    :  393923440
  </code></pre>
  
  <p>Source code:</p>
  
  <pre><code>func DoSomeWork(work chan int) {
      for _ = range work {
      }
  }
  
  func main() {
      k := make(chan int)
      m := &amp;runtime.MemStats{}
      runtime.ReadMemStats(m)
      fmt.Println("Memory Acquired: ", m.Sys)
      fmt.Println("Memory Used    : ", m.Alloc)
  
      wg := new(sync.WaitGroup)
      // generate a lot of goroutines that reads from channel
      for i:=0;i&lt;1000000;i++ {
          wg.Add(1)
          go func() {
              DoSomeWork(k)
              wg.Done()
          }()
      }
  
      close(k)
      wg.Wait()
  
      // make GC
      runtime.GC()
  
      // show memory after garbage collector
      runtime.ReadMemStats(m)
      fmt.Println("Memory Acquired: ", m.Sys)
      fmt.Println("Memory Used    : ", m.Alloc)
  }
  </code></pre>