I've been playing a bit with Go to check the garbage collector, and I found
an unexpected behaviour (unexpected for me). I don't know if it is a bug or
something that I don't really understand.
Everything has been tested on tip version.
Program is here: http://play.golang.org/p/QbfGh5xnhF
package main
import (
"time"
// "runtime/debug"
)
func main() {
f()
// debug.FreeOSMemory()
// debug.FreeOSMemory()
time.Sleep(10 * time.Hour)
}
func f() {
a := make([]int, 100000000)
for i := range a {
a[i] = i
}
}
The program allocates 763 MB of RAM (on 64-bits architecture) and then
sleeps for 10 hours. I've been monitoring the memory used and the GC work
by setting GODEBUG=gotrace=1
With the FreeOSMemory() lines commented, the memory is never GC'd nor
released back to the system. I've waited about 30 minutes.
With one single line of FreeOSMemory(), the memory is not freed immediately
(as I expected) but it seems that in the 2-3 next automatic GC runs it is
finally found and freed, and eventually, released back to the OS.
With two FreeOSMemory() lines, the memory is released immediately (I
suppose that in the second call).
I understand that this scenario is not very real. If I do some other work
instead of just waiting, eventually the GC is executed, there is more
garbage than my slice to be purged and everything is released.
Is this expected? In particular, that the 763 MB are not GC'd automatically
without FreeOSMemery() lines.
Thanks
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.