Skip to content
Permalink
Browse files

Use malloc_trim

  • Loading branch information
DarthSim committed Jan 10, 2020
1 parent 4f178cf commit c0fb6b1435fe2147530b1129c62ff817f9f50aed
Showing with 30 additions and 2 deletions.
  1. +9 −0 free_memory.go
  2. +20 −0 free_memory_linux.go
  3. +1 −2 main.go
@@ -0,0 +1,9 @@
// +build !linux

package main

import "runtime/debug"

func freeMemory() {
debug.FreeOSMemory()
}
@@ -0,0 +1,20 @@
// +build linux

package main

/*
#include <features.h>
#ifdef __GLIBC__
#include <malloc.h>
#else
void malloc_trim(size_t pad){}
#endif
*/
import "C"
import "runtime/debug"

func freeMemory() {
debug.FreeOSMemory()

C.malloc_trim(0)
}
@@ -5,7 +5,6 @@ import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"
"time"
)
@@ -38,7 +37,7 @@ func main() {
var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0

for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
debug.FreeOSMemory()
freeMemory()

if logMemStats {
var m runtime.MemStats

0 comments on commit c0fb6b1

Please sign in to comment.
You can’t perform that action at this time.