From 69b5dbdcc3e46adaeda013d8af06ec0de7320947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 11 Jul 2023 21:22:33 +0800 Subject: [PATCH] Build memory limiter for android --- experimental/libbox/memory.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/experimental/libbox/memory.go b/experimental/libbox/memory.go index 173eaf7d..b3c72570 100644 --- a/experimental/libbox/memory.go +++ b/experimental/libbox/memory.go @@ -1,18 +1,22 @@ -//go:build darwin - package libbox import ( + "math" runtimeDebug "runtime/debug" "github.com/sagernet/sing-box/common/dialer/conntrack" ) -const memoryLimit = 30 * 1024 * 1024 - -func SetMemoryLimit() { - runtimeDebug.SetGCPercent(10) - runtimeDebug.SetMemoryLimit(memoryLimit) - conntrack.KillerEnabled = true - conntrack.MemoryLimit = memoryLimit +func SetMemoryLimit(enabled bool) { + const memoryLimit = 30 * 1024 * 1024 + if enabled { + runtimeDebug.SetGCPercent(10) + runtimeDebug.SetMemoryLimit(memoryLimit) + conntrack.KillerEnabled = true + conntrack.MemoryLimit = memoryLimit + } else { + runtimeDebug.SetGCPercent(100) + runtimeDebug.SetMemoryLimit(math.MaxInt64) + conntrack.KillerEnabled = false + } }