Golang Garbage Collector 101
Take a look at GC running visually
Quick Look at Garbage Collector
package main
import (
"fmt"
"runtime"
"time"
)
func printStats(mem runtime.MemStats) {
runtime.ReadMemStats(&mem)
fmt.Println("mem.Alloc:", mem.Alloc)
fmt.Println("mem.TotalAlloc:", mem.TotalAlloc)
fmt.Println("mem.HeapAlloc:", mem.HeapAlloc)
fmt.Println("mem.NumGC:", mem.NumGC)
fmt.Println("-----")
}
func main() {
var mem runtime.MemStats
for i := 0; i < 2; i++ {
s := make([]byte, 100000000)
if s == nil {
fmt.Println("Operation failed!")
}
printStats(mem)
}
time.Sleep(time.Second)
// adding time.Sleep so that GC finishes it works and print out the output to terminal
}Understanding the code
func ReadMemStats
Last updated