Measuring memory usage of executable run using golang

南笙酒味 提交于 2020-01-02 05:45:07

问题


How do I measure the amount of memory used by an executable which I run through the os/exec package in Golang? Is it better to do this through the OS itself?


回答1:


You need to do this through the OS itself. If you are on plan9 or posix, Go will return the usage values from the OS for you in the structure returned by 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. Check man getrusage for details.



来源:https://stackoverflow.com/questions/27625787/measuring-memory-usage-of-executable-run-using-golang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!