memory.go 520 B

1234567891011121314151617181920212223
  1. package libbox
  2. import (
  3. "math"
  4. runtimeDebug "runtime/debug"
  5. "github.com/sagernet/sing-box/common/conntrack"
  6. )
  7. func SetMemoryLimit(enabled bool) {
  8. const memoryLimit = 45 * 1024 * 1024
  9. const memoryLimitGo = memoryLimit / 1.5
  10. if enabled {
  11. runtimeDebug.SetGCPercent(10)
  12. runtimeDebug.SetMemoryLimit(memoryLimitGo)
  13. conntrack.KillerEnabled = true
  14. conntrack.MemoryLimit = memoryLimit
  15. } else {
  16. runtimeDebug.SetGCPercent(100)
  17. runtimeDebug.SetMemoryLimit(math.MaxInt64)
  18. conntrack.KillerEnabled = false
  19. }
  20. }