GoBlog/garbagecollector.go

30 lines
488 B
Go
Raw Normal View History

2021-03-13 12:17:42 +00:00
package main
import (
"log"
"runtime"
"time"
)
func initGC() {
go func() {
ticker := time.NewTicker(15 * time.Minute)
2021-03-13 12:17:42 +00:00
for range ticker.C {
doGC()
2021-03-13 12:17:42 +00:00
}
}()
}
func doGC() {
2022-02-26 19:38:52 +00:00
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
2021-03-13 12:17:42 +00:00
runtime.GC()
2022-02-26 19:38:52 +00:00
runtime.ReadMemStats(&after)
log.Printf(
"\nAlloc: %d MiB -> %d MiB\nSys: %d MiB -> %d MiB\nNumGC: %d",
2022-02-26 19:38:52 +00:00
before.Alloc/1024/1024, after.Alloc/1024/1024,
before.Sys/1024/1024, after.Sys/1024/1024,
after.NumGC,
)
2021-03-13 12:17:42 +00:00
}