mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-23 17:11:29 +00:00
39 lines
732 B
Go
39 lines
732 B
Go
package conntrack
|
|
|
|
import (
|
|
"runtime"
|
|
runtimeDebug "runtime/debug"
|
|
"time"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
)
|
|
|
|
var (
|
|
KillerEnabled bool
|
|
MemoryLimit int64
|
|
killerLastCheck time.Time
|
|
)
|
|
|
|
func killerCheck() error {
|
|
if !KillerEnabled {
|
|
return nil
|
|
}
|
|
nowTime := time.Now()
|
|
if nowTime.Sub(killerLastCheck) < 3*time.Second {
|
|
return nil
|
|
}
|
|
killerLastCheck = nowTime
|
|
var memStats runtime.MemStats
|
|
runtime.ReadMemStats(&memStats)
|
|
inuseMemory := int64(memStats.StackInuse + memStats.HeapInuse + memStats.HeapIdle - memStats.HeapReleased)
|
|
if inuseMemory > MemoryLimit {
|
|
Close()
|
|
go func() {
|
|
time.Sleep(time.Second)
|
|
runtimeDebug.FreeOSMemory()
|
|
}()
|
|
return E.New("out of memory")
|
|
}
|
|
return nil
|
|
}
|