Each c = make([]int64, 10000000000
is attempting to allocate 80GB (8 * 10,000,000,000 bytes) of memory. Use a reasonable sized allocation (relative to the size of your real memory) and everything works as expected. For example,
package main
import (
"fmt"
"log"
"runtime"
)
func main() {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
fmt.Println(ms.TotalAlloc, ms.Alloc)
var c []int64
for i := 0; i < 100; i++ {
c = make([]int64, 400000000)
log.Println(len(c), i)
}
runtime.ReadMemStats(&ms)
fmt.Println(ms.TotalAlloc, ms.Alloc)
}
Output:
67032 67032
2017/11/23 01:13:08 400000000 0
2017/11/23 01:13:09 400000000 1
2017/11/23 01:13:09 400000000 2
2017/11/23 01:13:09 400000000 3
2017/11/23 01:13:10 400000000 4
<<SNIP>>
2017/11/23 01:13:43 400000000 95
2017/11/23 01:13:43 400000000 96
2017/11/23 01:13:43 400000000 97
2017/11/23 01:13:44 400000000 98
2017/11/23 01:13:44 400000000 99
320000171152 88168
You tried to allocate 80,000,000,000 bytes for c. I reduced it to something more reasonable 3,200,000,000 bytes. The loop allocated 100 times for a total of 320,000,171,152 bytes, which the garbage collector handled by reusing memory. The GC is working.