Build memory limiter for android

This commit is contained in:
世界 2023-07-11 21:22:33 +08:00
parent 1121517755
commit 69b5dbdcc3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -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
}
}