memory.go 469 B

1234567891011121314151617181920212223242526
  1. package libbox
  2. import (
  3. "math"
  4. runtimeDebug "runtime/debug"
  5. C "github.com/sagernet/sing-box/constant"
  6. )
  7. var memoryLimitEnabled bool
  8. func SetMemoryLimit(enabled bool) {
  9. memoryLimitEnabled = enabled
  10. const memoryLimitGo = 45 * 1024 * 1024
  11. if enabled {
  12. runtimeDebug.SetGCPercent(10)
  13. if C.IsIos {
  14. runtimeDebug.SetMemoryLimit(memoryLimitGo)
  15. }
  16. } else {
  17. runtimeDebug.SetGCPercent(100)
  18. if C.IsIos {
  19. runtimeDebug.SetMemoryLimit(math.MaxInt64)
  20. }
  21. }
  22. }