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"
|
|
|
|
|
2023-09-20 14:12:08 +08:00
|
|
|
"github.com/sagernet/sing-box/common/conntrack"
|
2023-03-16 10:55:06 +08:00
|
|
|
)
|
|
|
|
|
2023-07-11 21:22:33 +08:00
|
|
|
func SetMemoryLimit(enabled bool) {
|
2023-09-20 14:12:08 +08:00
|
|
|
const memoryLimit = 45 * 1024 * 1024
|
|
|
|
const memoryLimitGo = memoryLimit / 1.5
|
2023-07-11 21:22:33 +08:00
|
|
|
if enabled {
|
|
|
|
runtimeDebug.SetGCPercent(10)
|
2023-09-20 14:12:08 +08:00
|
|
|
runtimeDebug.SetMemoryLimit(memoryLimitGo)
|
2023-07-11 21:22:33 +08:00
|
|
|
conntrack.KillerEnabled = true
|
|
|
|
conntrack.MemoryLimit = memoryLimit
|
|
|
|
} else {
|
|
|
|
runtimeDebug.SetGCPercent(100)
|
|
|
|
runtimeDebug.SetMemoryLimit(math.MaxInt64)
|
|
|
|
conntrack.KillerEnabled = false
|
|
|
|
}
|
2023-03-02 13:13:12 +08:00
|
|
|
}
|