mirror of https://github.com/jlelse/GoBlog
Replace GC ticker with 100 MB memory limit, should be enough
This commit is contained in:
parent
93d176676f
commit
c559cf7ea9
|
@ -1,29 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"runtime/debug"
|
||||||
"runtime"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initGC() {
|
func initGC() {
|
||||||
go func() {
|
// Set memory limit to 100 MB
|
||||||
ticker := time.NewTicker(15 * time.Minute)
|
debug.SetMemoryLimit(100 * 1000 * 1000)
|
||||||
for range ticker.C {
|
|
||||||
doGC()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func doGC() {
|
|
||||||
var before, after runtime.MemStats
|
|
||||||
runtime.ReadMemStats(&before)
|
|
||||||
runtime.GC()
|
|
||||||
runtime.ReadMemStats(&after)
|
|
||||||
log.Printf(
|
|
||||||
"\nAlloc: %d MiB -> %d MiB\nSys: %d MiB -> %d MiB\nNumGC: %d",
|
|
||||||
before.Alloc/1024/1024, after.Alloc/1024/1024,
|
|
||||||
before.Sys/1024/1024, after.Sys/1024/1024,
|
|
||||||
after.NumGC,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue