Home > database >  how to get golang rss from runtime.MemStats
how to get golang rss from runtime.MemStats

Time:02-24

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(github.com/shirou/gopsutil/process), 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 (
    "fmt"
    "github.com/gin-gonic/gin"
    "io/ioutil"
    "log"
    "os"

    "github.com/dustin/go-humanize"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/shirou/gopsutil/process"

    "runtime"
    "strconv"
    "strings"
    "syscall"
    "time"
)

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 {
        log.Fatal(err)
    }
    fs := strings.Fields(string(data))
    rss, err := strconv.ParseInt(fs[23], 10, 64)
    if err != nil {
        log.Fatal(err)
    }
    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
            runtime.ReadMemStats(&m)
            fmt.Printf("rss:%s\n", humanize.Bytes(m.HeapSys-m.HeapReleased))
            getPs(pid)
            fmt.Println("rss by file:MB", rss())
            fmt.Println()
            time.Sleep(10 * time.Second)
        }
    }()
    r := gin.Default()
    r.GET("/metrics", gin.WrapH(promhttp.Handler()))
    log.Fatal(r.Run())
}

*/

output like this

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

CodePudding user response:

[H]ow to get golang rss from runtime.MemStats [?]

You cannot. runtime.Memstats doesn't provide that information.

why m.HeapSys-m.HeapReleased, and m.Sys not equal the rss [?]

Because RSS is a completely different metric.

CodePudding user response:

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

  • Related