Skip to content

Commit 3b3658d

Browse files
committed
tun2socks: enforce min gc memlimit 512mib
1 parent 056e022 commit 3b3658d

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

intra/tun2socks.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ const (
5555

5656
func (t traceout) s() string { return string(t) }
5757

58+
const minMemLimit = 512 * 1024 * 1024 // 512MiB
59+
const maxMemLimit = 4 * 1024 * 1024 * 1024 // 4GiB
60+
5861
func init() {
5962
// increase garbage collection frequency: archive.is/WQBf7
6063
debug.SetGCPercent(25)
61-
debug.SetMemoryLimit(1024 * 1024 * 1024 * 4) // 4GB
64+
debug.SetMemoryLimit(maxMemLimit)
6265
debug.SetPanicOnFault(true)
6366
}
6467

@@ -152,6 +155,7 @@ func FlightRecorder(y bool) (bool, error) {
152155
// LowMem triggers garbage collection cycle & allows for
153156
// setting maximum memory limit, if limit > 0.
154157
func LowMem(limitBytes int64) {
158+
limitBytes = max(limitBytes, minMemLimit)
155159
prevLimit := debug.SetMemoryLimit(limitBytes)
156160
go debug.FreeOSMemory()
157161
log.I("tun: lowmem; limits => new: %d, prev: %d", limitBytes, prevLimit)

0 commit comments

Comments
 (0)