sing-box/experimental/libbox/memory.go

24 lines
520 B
Go
Raw Normal View History

2023-03-02 05:13:12 +00:00
package libbox
2023-03-16 02:55:06 +00:00
import (
2023-07-11 13:22:33 +00:00
"math"
2023-03-16 02:55:06 +00:00
runtimeDebug "runtime/debug"
2023-09-20 06:12:08 +00:00
"github.com/sagernet/sing-box/common/conntrack"
2023-03-16 02:55:06 +00:00
)
2023-07-11 13:22:33 +00:00
func SetMemoryLimit(enabled bool) {
2023-09-20 06:12:08 +00:00
const memoryLimit = 45 * 1024 * 1024
const memoryLimitGo = memoryLimit / 1.5
2023-07-11 13:22:33 +00:00
if enabled {
runtimeDebug.SetGCPercent(10)
2023-09-20 06:12:08 +00:00
runtimeDebug.SetMemoryLimit(memoryLimitGo)
2023-07-11 13:22:33 +00:00
conntrack.KillerEnabled = true
conntrack.MemoryLimit = memoryLimit
} else {
runtimeDebug.SetGCPercent(100)
runtimeDebug.SetMemoryLimit(math.MaxInt64)
conntrack.KillerEnabled = false
}
2023-03-02 05:13:12 +00:00
}