From 0e8f08476cc3ae0996ed5d814892b8e342cb2e7f Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Wed, 24 Jan 2018 14:51:13 -0800 Subject: [PATCH] cmd/containerd-shim: aggressive memory reclamation To avoid having the shim hold on to too much memory, we've made a few adjustments to favor more aggressive reclamation of memory from the operating system. Typically, this would be negligible, on the order of a few megabytes, but this is impactful when running several containers. The first fix is to lower the threshold used to determine when to run the garbage collector. The second runs `runtime/debug.FreeOSMemory` at a regular interval. Under test, this result in a sustained memory usage of around 3.7 MB. Signed-off-by: Stephen J Day --- cmd/containerd-shim/main_unix.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/containerd-shim/main_unix.go b/cmd/containerd-shim/main_unix.go index e1996fd92..307d44899 100644 --- a/cmd/containerd-shim/main_unix.go +++ b/cmd/containerd-shim/main_unix.go @@ -12,9 +12,11 @@ import ( "os/exec" "os/signal" "runtime" + "runtime/debug" "strings" "sync" "syscall" + "time" "github.com/containerd/containerd/events" "github.com/containerd/containerd/linux/proc" @@ -58,6 +60,13 @@ func init() { } func main() { + debug.SetGCPercent(10) + go func() { + for range time.Tick(30 * time.Second) { + debug.FreeOSMemory() + } + }() + if debugFlag { logrus.SetLevel(logrus.DebugLevel) }