Browse Source

Use malloc_trim

DarthSim 5 years ago
parent
commit
c0fb6b1435
3 changed files with 30 additions and 2 deletions
  1. 9 0
      free_memory.go
  2. 20 0
      free_memory_linux.go
  3. 1 2
      main.go

+ 9 - 0
free_memory.go

@@ -0,0 +1,9 @@
+// +build !linux
+
+package main
+
+import "runtime/debug"
+
+func freeMemory() {
+	debug.FreeOSMemory()
+}

+ 20 - 0
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)
+}

+ 1 - 2
main.go

@@ -5,7 +5,6 @@ import (
 	"os"
 	"os"
 	"os/signal"
 	"os/signal"
 	"runtime"
 	"runtime"
-	"runtime/debug"
 	"syscall"
 	"syscall"
 	"time"
 	"time"
 )
 )
@@ -38,7 +37,7 @@ func main() {
 		var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
 		var logMemStats = len(os.Getenv("IMGPROXY_LOG_MEM_STATS")) > 0
 
 
 		for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
 		for range time.Tick(time.Duration(conf.FreeMemoryInterval) * time.Second) {
-			debug.FreeOSMemory()
+			freeMemory()
 
 
 			if logMemStats {
 			if logMemStats {
 				var m runtime.MemStats
 				var m runtime.MemStats