Optimized memory usage; Reducing memory fragmentation

This commit is contained in:
DarthSim
2019-01-17 14:51:19 +06:00
parent 1ed81cf2e6
commit f4f746489c
9 changed files with 206 additions and 59 deletions

17
main.go
View File

@@ -3,6 +3,9 @@ package main
import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"time"
"net/http"
_ "net/http/pprof"
@@ -13,6 +16,20 @@ const version = "2.1.5"
type ctxKey string
func main() {
go func() {
var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
debug.FreeOSMemory()
if logMemStats {
var m runtime.MemStats
runtime.ReadMemStats(&m)
logNotice("[MEMORY USAGE] Sys: %d; HeapIdle: %d; HeapInuse: %d", m.Sys/1024/1024, m.HeapIdle/1024/1024, m.HeapInuse/1024/1024)
}
}
}()
if len(os.Getenv("IMGPROXY_PPROF_BIND")) > 0 {
go func() {
http.ListenAndServe(os.Getenv("IMGPROXY_PPROF_BIND"), nil)