I need to know physical memory consumption of my Go process in Linux. What I need is Resident Set Size (RSS).
runtime.ReadMemStats
is not suitable because there is no way to get a size of a portion of a virtual memory that is resident.
syscall.Getrusage
is not suitable because it returns only Maxrss
that is maximal RSS during process life.
How can I query RSS in Go?
Advertisement
Answer
You can parse /proc/self/statm file. It contains only integers so it is easy to parse. Second field is RSS measured in pages.
func getRss() (int64, error) { buf, err := ioutil.ReadFile("/proc/self/statm") if err != nil { return 0, err } fields := strings.Split(string(buf), " ") if len(fields) < 2 { return 0, errors.New("Cannot parse statm") } rss, err := strconv.ParseInt(fields[1], 10, 64) if err != nil { return 0, err } return rss * int64(os.Getpagesize()), err }