I've got a nasty memory leak. It appears that Go won't reclaim memory when 
goroutines are called in a highly parallel fashion. 

To illustrate the issue I've written a simple example below. When I run 
this on my Windows system, I get 800MB to 1 GB of memory consumption. 

What's fascinating is that if I change the time.Sleep to 100 milliseconds 
instead of 1, the process never goes above 6 MB of memory. This is what I 
would expect since there are no objects here that should be retained in 
memory. And, it doesn't matter how long I wait, the garbage collector never 
cleans up the mess either.

I've tried to profile it using pprof and it didn't help me. It helped me 
find and fix other issues, but not this. It's entirely possible I did it 
wrong though since I am new to using that tool.

Help! Thanks in advance!

package main

import (
  "bytes"
  "fmt"
  "io/ioutil"
  "math/rand"
  "runtime"
  "time"
)

func main() {
  for i := 0; i < 1000; i++ {
    time.Sleep(time.Millisecond * 1)
    go fakeGetAndSaveData()
  }
  runtime.GC()
  time.Sleep(10 * time.Minute)
}

func fakeGetAndSaveData() {
  var buf bytes.Buffer
  for i := 0; i < 40000; i++ {
    buf.WriteString(fmt.Sprintf("the number is %d\n", i))
  }

  ioutil.WriteFile(fmt.Sprintf("%d.txt", rand.Int()), buf.Bytes(), 0644)
}

Thanks,
Rob Archibald

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to