6

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?

2
  • 4
    Real-time or post-execution? /usr/bin/time -f "%M" ls could be used if you want the result. Commented Dec 23, 2014 at 18:17
  • Is there any cross-platform solution for this? Commented Dec 23, 2014 at 18:36

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Any thoughts on a real-time solution? i.e., before the process has existed. I think cmd.ProcessState is only available after the program has terminated.
@KevinBullaughey: see stackoverflow.com/questions/31879817/…. Everything is available in /proc/$PID/smaps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.