I wanted to check my program for go routine and memory leaks. In doing so I detected what resemble a memory leak while my program was doing nothing.
Here is a minimal program that reproduces the problem. The program collects and prints the total number of bytes allocated and the number of blocks.
package main
import (
"runtime"
func main() {
var m runtime.MemStats
ticker := time.NewTicker(20 * time.Second)
for {
runtime.ReadMemStats(&m)
println("status: memUsed:", m.Alloc, "allocs:", m.Mallocs-m.Frees, "numGC", m.NumGC)
<-ticker.C
runtime.GC() }
}
What I see is a slow but steady increase of memUse and allocs. The memUse grows by 4 to 96 bytes every 40 to 60 seconds.
Is this a feature of the GC or is this a memory leak in one of the called functions ?
Note that I use println because the "leak" is much more important when I use fmt.Println of log.Println. I also use ticker because I was told it would be better than time.Sleep, but I don’t see any significant difference in the "leak" when I use one or the other.