I'm trying to release the slice completely, so the memory goes back to the OS; however, when I try to dereference it by doing slice = nil
, nothing happens.
I have tried also using runtime.GC()
and debug.FreeOSMemory()
, but neither of those helped.
Here's the code I have:
package main
import (
"fmt"
"runtime"
"runtime/debug"
)
func printMemStats(stage string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Println(stage, m.Sys/1024/1024)
}
func main() {
printMemStats("before")
a := make([]uint64, 1000000000)
printMemStats("after")
a = nil
runtime.GC()
debug.FreeOSMemory()
printMemStats("remove")
_ = a // get rid of the unused variable error
}
That results in:
before 68
after 7997
remove 7998
Whereas the expected output is:
before 68
after 7997
remove 68