Skip to content

Commit

Permalink
Use malloc_trim
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Jan 10, 2020
1 parent 4f178cf commit c0fb6b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions free_memory.go
@@ -0,0 +1,9 @@
// +build !linux

package main

import "runtime/debug"

func freeMemory() {
debug.FreeOSMemory()
}
20 changes: 20 additions & 0 deletions free_memory_linux.go
@@ -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)
}
3 changes: 1 addition & 2 deletions main.go
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"
"time"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c0fb6b1

Please sign in to comment.