浏览代码

Build memory limiter for android

世界 2 年之前
父节点
当前提交
69b5dbdcc3
共有 1 个文件被更改,包括 13 次插入9 次删除
  1. 13 9
      experimental/libbox/memory.go

+ 13 - 9
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
+	}
 }