Measuring memory usage of an executable using golang

How to measure the amount of memory used by an executable file that I run through a package os/execin the Golang? Is it better to do this using the OS itself?

+4
source share
1 answer

You need to do this using the OS itself. If you are on plan9 or posix, Go will return usage values ​​from the OS for you in the structure returned ProcessState.SysUsage().

cmd := exec.Command("command", "arg1", "arg2")
err := cmd.Run()
if err != nil {
    log.Fatal(err)
}
// check this type assertion to avoid a panic
fmt.Println("MaxRSS:", cmd.ProcessState.SysUsage().(*syscall.Rusage).Maxrss)

Note: different platforms may return this in bytes or kilobytes. See more details man getrusage.

+6
source

Source: https://habr.com/ru/post/1568800/


All Articles