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).
This commit is contained in:
MHSanaei
2026-06-25 22:16:38 +02:00
parent b32837e523
commit 69ad8b76e1
7 changed files with 138 additions and 38 deletions
+19
View File
@@ -0,0 +1,19 @@
package job
import "runtime/debug"
// MemoryReleaseJob returns freed heap spans to the OS so steady-state RSS tracks
// the live heap between the bursty traffic-collection jobs, instead of lingering
// at the high-water mark until the scavenger lazily reclaims it.
type MemoryReleaseJob struct{}
// NewMemoryReleaseJob creates a new memory-release job instance.
func NewMemoryReleaseJob() *MemoryReleaseJob {
return new(MemoryReleaseJob)
}
// Run forces a GC and returns as much free memory to the OS as possible. It is
// scheduled on a minutes cadence because FreeOSMemory triggers a full GC.
func (j *MemoryReleaseJob) Run() {
debug.FreeOSMemory()
}
+7 -3
View File
@@ -610,9 +610,13 @@ func (s *ServerService) GetStatus(lastStatus *Status) *Status {
}
// Application stats
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
status.AppStats.Mem = rtm.Sys
if rss := sys.SelfRSS(); rss > 0 {
status.AppStats.Mem = rss
} else {
var rtm runtime.MemStats
runtime.ReadMemStats(&rtm)
status.AppStats.Mem = rtm.Sys
}
status.AppStats.Threads = uint32(runtime.NumGoroutine())
if p != nil && p.IsRunning() {
status.AppStats.Uptime = p.GetUptime()
+5
View File
@@ -21,6 +21,7 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/mtproto"
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
"github.com/mhsanaei/3x-ui/v3/internal/util/sys"
"github.com/mhsanaei/3x-ui/v3/internal/web/controller"
"github.com/mhsanaei/3x-ui/v3/internal/web/job"
"github.com/mhsanaei/3x-ui/v3/internal/web/locale"
@@ -394,6 +395,10 @@ func (s *Server) startTask(restartXray bool) {
if s.memoryAlarmWanted() {
s.cron.AddJob(cadenceMemoryAlarm, job.NewCheckMemJob())
}
if mins := sys.MemoryReleaseIntervalMinutes(); mins > 0 {
s.cron.AddJob(fmt.Sprintf("@every %dm", mins), job.NewMemoryReleaseJob())
}
}
// cpuAlarmWanted reports whether any notifier is configured to receive cpu.high