1
0

procmem.go 795 B

12345678910111213141516171819202122232425262728293031323334
  1. package sys
  2. import (
  3. "os"
  4. "sync"
  5. "github.com/shirou/gopsutil/v4/process"
  6. )
  7. var (
  8. selfProc *process.Process
  9. selfProcOnce sync.Once
  10. )
  11. // SelfRSS returns the resident set size of the current process in bytes — the
  12. // real physical memory the OS attributes to the panel. Unlike
  13. // runtime.MemStats.Sys (a never-shrinking high-water mark of reserved address
  14. // space that also counts memory already returned to the OS), RSS reflects current
  15. // usage and drops as memory is released. Returns 0 when unavailable.
  16. func SelfRSS() uint64 {
  17. selfProcOnce.Do(func() {
  18. if p, err := process.NewProcess(int32(os.Getpid())); err == nil {
  19. selfProc = p
  20. }
  21. })
  22. if selfProc == nil {
  23. return 0
  24. }
  25. if mi, err := selfProc.MemoryInfo(); err == nil && mi != nil {
  26. return mi.RSS
  27. }
  28. return 0
  29. }