Files
sing-box-extended-mirror/experimental/libbox/memory.go
T

27 lines
469 B
Go
Raw Normal View History

2023-03-02 13:13:12 +08:00
package libbox
2023-03-16 10:55:06 +08:00
import (
2023-07-11 21:22:33 +08:00
"math"
2023-03-16 10:55:06 +08:00
runtimeDebug "runtime/debug"
2026-02-26 12:54:00 +08:00
C "github.com/sagernet/sing-box/constant"
2023-03-16 10:55:06 +08:00
)
2026-02-26 12:54:00 +08:00
var memoryLimitEnabled bool
2023-07-11 21:22:33 +08:00
func SetMemoryLimit(enabled bool) {
2026-02-26 12:54:00 +08:00
memoryLimitEnabled = enabled
const memoryLimitGo = 45 * 1024 * 1024
2023-07-11 21:22:33 +08:00
if enabled {
runtimeDebug.SetGCPercent(10)
2026-02-26 12:54:00 +08:00
if C.IsIos {
runtimeDebug.SetMemoryLimit(memoryLimitGo)
}
2023-07-11 21:22:33 +08:00
} else {
runtimeDebug.SetGCPercent(100)
2026-02-26 12:54:00 +08:00
if C.IsIos {
runtimeDebug.SetMemoryLimit(math.MaxInt64)
}
2023-07-11 21:22:33 +08:00
}
2023-03-02 13:13:12 +08:00
}