Improve oom-killer

This commit is contained in:
世界
2026-04-28 08:18:08 +08:00
parent fd1624e426
commit b418ee1a98
7 changed files with 45 additions and 10 deletions
+3
View File
@@ -856,6 +856,9 @@ func (r *Router) ClearCache() {
if r.platformInterface != nil {
r.platformInterface.ClearDNSCache()
}
if r.dnsReverseMapping != nil {
r.dnsReverseMapping.Purge()
}
}
func (r *Router) LookupReverseMapping(ip netip.Addr) (string, bool) {
+4
View File
@@ -21,6 +21,7 @@ import (
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/cleanup"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
N "github.com/sagernet/sing/common/network"
@@ -51,6 +52,7 @@ type Server struct {
trafficManager *trafficontrol.Manager
urlTestHistory adapter.URLTestHistoryStorage
logDebug bool
cleaner *cleanup.Cleaner
mode string
modeList []string
@@ -82,6 +84,7 @@ func NewServer(ctx context.Context, logFactory log.ObservableFactory, options op
externalController: options.ExternalController != "",
externalUIDownloadURL: options.ExternalUIDownloadURL,
externalUIDownloadDetour: options.ExternalUIDownloadDetour,
cleaner: cleanup.Add(trafficManager.Clear),
}
s.urlTestHistory = service.FromContext[adapter.URLTestHistoryStorage](ctx)
if s.urlTestHistory == nil {
@@ -193,6 +196,7 @@ func (s *Server) Close() error {
common.PtrOrNil(s.httpServer),
s.trafficManager,
s.urlTestHistory,
common.PtrOrNil(s.cleaner),
)
}
@@ -159,9 +159,10 @@ func (m *Manager) Snapshot() *Snapshot {
}
}
func (m *Manager) ResetStatistic() {
m.uploadTotal.Store(0)
m.downloadTotal.Store(0)
func (m *Manager) Clear() {
m.closedConnectionsAccess.Lock()
defer m.closedConnectionsAccess.Unlock()
m.closedConnections.Init()
}
type Snapshot struct {
+19
View File
@@ -0,0 +1,19 @@
//go:build badlinkname
package oomkiller
import (
"sync"
_ "unsafe"
)
//go:linkname jsonFieldCache json.fieldCache
var jsonFieldCache sync.Map
//go:linkname contextJSONFieldCache github.com/sagernet/sing/common/json/internal/contextjson.fieldCache
var contextJSONFieldCache sync.Map
func badCleanup() {
jsonFieldCache.Clear()
contextJSONFieldCache.Clear()
}
+6
View File
@@ -0,0 +1,6 @@
//go:build !badlinkname
package oomkiller
func badCleanup() {
}
-2
View File
@@ -33,7 +33,6 @@ static void stopMemoryPressureMonitor() {
import "C"
import (
runtimeDebug "runtime/debug"
"sync"
"github.com/sagernet/sing-box/adapter"
@@ -90,7 +89,6 @@ func (s *Service) Close() error {
//export goMemoryPressureCallback
func goMemoryPressureCallback(status C.ulong) {
runtimeDebug.FreeOSMemory()
globalAccess.Lock()
services := make([]*Service, len(globalServices))
copy(services, globalServices)
+9 -5
View File
@@ -105,6 +105,7 @@ type adaptiveTimer struct {
limitThresholds pressureThresholds
access sync.Mutex
cleanupTriggered bool
timer *time.Timer
state pressureState
currentInterval time.Duration
@@ -161,10 +162,6 @@ func (t *adaptiveTimer) stop() {
}
func (t *adaptiveTimer) poll() {
if t.timerConfig.policyMode == policyModeNetworkExtension {
runtimeDebug.FreeOSMemory()
}
var triggered bool
var rateTriggered bool
sample := readMemorySample(t.policyMode)
@@ -174,6 +171,12 @@ func (t *adaptiveTimer) poll() {
t.access.Unlock()
return
}
if t.timerConfig.policyMode == policyModeNetworkExtension {
if t.cleanupTriggered {
runtimeDebug.FreeOSMemory()
t.cleanupTriggered = true
}
}
if t.pendingPressureBaseline {
t.pressureBaseline = sample
t.pressureBaselineTime = time.Now()
@@ -205,10 +208,10 @@ func (t *adaptiveTimer) poll() {
}
}
t.access.Unlock()
if !triggered {
return
}
t.cleanupTriggered = false
t.onTriggered(sample.usage)
if rateTriggered {
if t.killerDisabled {
@@ -225,6 +228,7 @@ func (t *adaptiveTimer) poll() {
t.router.ResetNetwork()
}
}
badCleanup()
runtimeDebug.FreeOSMemory()
}