how to get golang rss from runtime.MemStats


i read the (go)mem info from runtime.MemStats, but cant found rss value, i use m.HeapSys-m.HeapReleased, but found the value is very not like rss ,also i dump the Rss by other tool(, i want to know how to get the rss by memstats, and why m.HeapSys-m.HeapReleased, and m.Sys not equal the rss, so different the value ?

package main

import (



var pageSize = syscall.Getpagesize()

// rss returns the resident set size of the current process, unit in MiB
func rss() int {
    data, err := ioutil.ReadFile("/proc/self/stat")
    if err != nil {
    fs := strings.Fields(string(data))
    rss, err := strconv.ParseInt(fs[23], 10, 64)
    if err != nil {
    return int(uintptr(rss) * uintptr(pageSize) / (1 << 20)) // MiB

func getPs(pid int) {
    ps, _ := process.Processes()
    for _, p := range ps {
        if p.Pid == int32(pid) {
            mem, _ := p.MemoryInfo()
            fmt.Printf("get by process rss:%s\n", humanize.Bytes(mem.RSS))
func main() {
    pid := os.Getpid()

    go func() {
        for {
            var m runtime.MemStats
            fmt.Printf("rss:%s\n", humanize.Bytes(m.HeapSys-m.HeapReleased))
            fmt.Println("rss by file:MB", rss())
            time.Sleep(10 * time.Second)
    r := gin.Default()
    r.GET("/metrics", gin.WrapH(promhttp.Handler()))


output like this

rss:6.6 MB
get by process rss:18 MB
rss by file:MB 17


The closest value is memstats.Sys ~= RSS, but they are not absolute equal , beacuse the rss contain some other like share lib,equal , and the
go memory alose can be from virtual memory,not all from rss

the RSS explation

Answered By – faker

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.