From 92433dbafcfd48774ad04033692c82179403f3e4 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 7 Sep 2026 07:31:32 +0800 Subject: [PATCH] chore: reduce gVisor memory usage --- config/config.go | 24 +++++++++++++++--------- go.mod | 2 +- go.sum | 4 ++-- hub/route/configs.go | 6 ++++++ listener/config/tun.go | 7 +++++++ listener/inbound/tun.go | 5 +++++ listener/sing_tun/server.go | 1 + 7 files changed, 37 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index c8c9e1c9d..b1c8a6564 100644 --- a/config/config.go +++ b/config/config.go @@ -324,6 +324,9 @@ type RawTun struct { // darwin special config RecvMsgX bool `yaml:"recvmsgx" json:"recvmsgx,omitempty"` SendMsgX bool `yaml:"sendmsgx" json:"sendmsgx,omitempty"` + + // gvisor special config (Non-public option; do not include it in the document.) + ProcessorsPerChannel int `yaml:"processors-per-channel" json:"processors-per-channel,omitempty"` } type RawTuicServer struct { @@ -538,15 +541,16 @@ func DefaultRawConfig() *RawConfig { Interval: 30, }, Tun: RawTun{ - Enable: false, - Device: "", - Stack: C.TunGvisor, - DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query - AutoRoute: true, - AutoDetectInterface: true, - Inet6Address: []netip.Prefix{netip.MustParsePrefix("fdfe:dcba:9876::1/126")}, - RecvMsgX: true, - SendMsgX: false, // In the current implementation, if enabled, the kernel may freeze during multi-thread downloads, so it is disabled by default. + Enable: false, + Device: "", + Stack: C.TunGvisor, + DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query + AutoRoute: true, + AutoDetectInterface: true, + Inet6Address: []netip.Prefix{netip.MustParsePrefix("fdfe:dcba:9876::1/126")}, + RecvMsgX: true, + SendMsgX: false, // In the current implementation, if enabled, the kernel may freeze during multi-thread downloads, so it is disabled by default. + ProcessorsPerChannel: 1, // For most users, memory usage is more important than peak performance. Setting this to 1 can significantly reduce memory consumption. }, TuicServer: RawTuicServer{ Enable: false, @@ -1732,6 +1736,8 @@ func parseTun(rawTun RawTun, dns *DNS, general *General) error { RecvMsgX: rawTun.RecvMsgX, SendMsgX: rawTun.SendMsgX, + + ProcessorsPerChannel: rawTun.ProcessorsPerChannel, } return nil diff --git a/go.mod b/go.mod index 3e2f12787..a1e648e82 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/metacubex/sing-quic v0.0.0-20260904234848-1c242664697a github.com/metacubex/sing-shadowsocks v0.2.12 github.com/metacubex/sing-shadowsocks2 v0.2.7 - github.com/metacubex/sing-tun v0.4.22 + github.com/metacubex/sing-tun v0.4.23 github.com/metacubex/sing-vmess v0.2.5 github.com/metacubex/sing-wireguard v0.0.0-20260826105301-c3ae17d19f9e github.com/metacubex/smux v0.0.0-20260105030934-d0c8756d3141 diff --git a/go.sum b/go.sum index 978ddb7fe..bd708eda0 100644 --- a/go.sum +++ b/go.sum @@ -161,8 +161,8 @@ github.com/metacubex/sing-shadowsocks v0.2.12 h1:Wqzo8bYXrK5aWqxu/TjlTnYZzAKtKsa github.com/metacubex/sing-shadowsocks v0.2.12/go.mod h1:2e5EIaw0rxKrm1YTRmiMnDulwbGxH9hAFlrwQLQMQkU= github.com/metacubex/sing-shadowsocks2 v0.2.7 h1:hSuuc0YpsfiqYqt1o+fP4m34BQz4e6wVj3PPBVhor3A= github.com/metacubex/sing-shadowsocks2 v0.2.7/go.mod h1:vOEbfKC60txi0ca+yUlqEwOGc3Obl6cnSgx9Gf45KjE= -github.com/metacubex/sing-tun v0.4.22 h1:6ARRJ2BIFD1u4r/DTMNcxaNuGyimfXEeUyD4iFJRaZs= -github.com/metacubex/sing-tun v0.4.22/go.mod h1:9T0SN+FHvzRrn7hgl2kmXy5v8xzzLKW1hITButIPAco= +github.com/metacubex/sing-tun v0.4.23 h1:k9QZ+GU0C8uCymXoXB8PZcQ/vEt4kXMuh620q3Ht8zE= +github.com/metacubex/sing-tun v0.4.23/go.mod h1:9T0SN+FHvzRrn7hgl2kmXy5v8xzzLKW1hITButIPAco= github.com/metacubex/sing-vmess v0.2.5 h1:m9Zt5I27lB9fmLMZfism9sH2LcnAfShZfwSkf6/KJoE= github.com/metacubex/sing-vmess v0.2.5/go.mod h1:AwtlzUgf8COe9tRYAKqWZ+leDH7p5U98a0ZUpYehl8Q= github.com/metacubex/sing-wireguard v0.0.0-20260826105301-c3ae17d19f9e h1:KGKZt//rrELT/dEwgYcHCqNYEGr3a7uVutlZrzrZZWY= diff --git a/hub/route/configs.go b/hub/route/configs.go index 18a2af489..9693403af 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -108,6 +108,9 @@ type tunSchema struct { // darwin special config RecvMsgX *bool `yaml:"recvmsgx" json:"recvmsgx,omitempty"` SendMsgX *bool `yaml:"sendmsgx" json:"sendmsgx,omitempty"` + + // gvisor special config (Non-public option; do not include it in the document.) + ProcessorsPerChannel *int `yaml:"processors-per-channel" json:"processors-per-channel,omitempty"` } type tuicServerSchema struct { @@ -270,6 +273,9 @@ func pointerOrDefaultTun(p *tunSchema, def LC.Tun) LC.Tun { if p.SendMsgX != nil { def.SendMsgX = *p.SendMsgX } + if p.ProcessorsPerChannel != nil { + def.ProcessorsPerChannel = *p.ProcessorsPerChannel + } } return def } diff --git a/listener/config/tun.go b/listener/config/tun.go index 9a7a119dc..f6ca47f09 100644 --- a/listener/config/tun.go +++ b/listener/config/tun.go @@ -63,6 +63,9 @@ type Tun struct { // darwin special config RecvMsgX bool `yaml:"recvmsgx" json:"recvmsgx,omitempty"` SendMsgX bool `yaml:"sendmsgx" json:"sendmsgx,omitempty"` + + // gvisor special config (Non-public option; do not include it in the document.) + ProcessorsPerChannel int `yaml:"processors-per-channel" json:"processors-per-channel,omitempty"` } func (t *Tun) Sort() { @@ -232,5 +235,9 @@ func (t *Tun) Equal(other Tun) bool { return false } + if t.ProcessorsPerChannel != other.ProcessorsPerChannel { + return false + } + return true } diff --git a/listener/inbound/tun.go b/listener/inbound/tun.go index e61afbfd2..477eb06da 100644 --- a/listener/inbound/tun.go +++ b/listener/inbound/tun.go @@ -64,6 +64,9 @@ type TunOption struct { // darwin special config RecvMsgX bool `inbound:"recvmsgx,omitempty"` SendMsgX bool `inbound:"sendmsgx,omitempty"` + + // gvisor special config (Non-public option; do not include it in the document.) + ProcessorsPerChannel int `inbound:"processors-per-channel,omitempty"` } var _ encoding.TextUnmarshaler = (*netip.Addr)(nil) // ensure netip.Addr can decode direct by structure package @@ -141,6 +144,8 @@ func NewTun(options *TunOption) (*Tun, error) { RecvMsgX: options.RecvMsgX, SendMsgX: options.SendMsgX, + + ProcessorsPerChannel: options.ProcessorsPerChannel, }, }, nil } diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 3e5ef5919..1e9527ec5 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -418,6 +418,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis InterfaceMonitor: defaultInterfaceMonitor, EXP_RecvMsgX: options.RecvMsgX, EXP_SendMsgX: options.SendMsgX, + EXP_ProcessorsPerChannel: options.ProcessorsPerChannel, } if options.AutoRedirect {