github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/memory.go (about)

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