Files
3x-ui/internal/util/sys/procmem.go
T
MHSanaei 69ad8b76e1 perf(memory): report real RSS and cut footprint via GOGC + periodic release
The Usage card showed runtime.MemStats.Sys, a never-shrinking high-water mark of reserved address space that also counts memory already returned to the OS, so it overstated real usage (e.g. ~300 MB on an idle 1-client server). Report process RSS instead so the number matches the OS and drops as memory is freed.

Replace the auto GOMEMLIMIT that targeted ~90 percent of total system RAM (a near no-op while the heap sits far below the limit, and a GC-thrash risk on small/shared VPS per go.dev/doc/gc-guide) with: a lower default GOGC (XUI_GOGC, default 75), a periodic debug.FreeOSMemory job (XUI_MEMORY_RELEASE_INTERVAL, default 10m, 0 disables), and a soft limit applied only from an explicit budget (GOMEMLIMIT, XUI_MEMORY_LIMIT, or a real cgroup cap at 90 percent).
2026-06-25 22:16:38 +02:00

35 lines
795 B
Go

package sys
import (
"os"
"sync"
"github.com/shirou/gopsutil/v4/process"
)
var (
selfProc *process.Process
selfProcOnce sync.Once
)
// SelfRSS returns the resident set size of the current process in bytes — the
// real physical memory the OS attributes to the panel. Unlike
// runtime.MemStats.Sys (a never-shrinking high-water mark of reserved address
// space that also counts memory already returned to the OS), RSS reflects current
// usage and drops as memory is released. Returns 0 when unavailable.
func SelfRSS() uint64 {
selfProcOnce.Do(func() {
if p, err := process.NewProcess(int32(os.Getpid())); err == nil {
selfProc = p
}
})
if selfProc == nil {
return 0
}
if mi, err := selfProc.MemoryInfo(); err == nil && mi != nil {
return mi.RSS
}
return 0
}