2

Currently i'm writing online judgment system on golang. To detect user program memory usage i've decided to analyze cmd.ProcessState.SysUsage() and check Rusage.Maxrss. Now i'm confused, because when i try to run this on my mac result of Rusage.Maxrss call is strange

Here is code, that i've runned on macOS and Linux (it's simplified, this code call Getrusage() of current process) And there're results i've gotten:

package main

import (
    "fmt"
    "syscall"
)

func main() {
    rusage := syscall.Rusage{}
    pageSize := syscall.Getpagesize()
    if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil {
        fmt.Println(err)
        panic(err)
    }

    fmt.Printf("page size: %d\nrusage.Maxrss: %d\n", pageSize, rusage.Maxrss)
}

And following results i've gotten

  • MacOS:
    go run test.go 
    page size: 4096
    rusage.Maxrss: 2007040
    
  • Linux/Ubuntu-18.04:
    go run test.go                     
    page size: 4096
    rusage.Maxrss: 17580
    

Can you explain why it returns such big value? As i've seen macOS manual and linux man pages: rusage.Maxrss (or rusage.ru_maxrss from C language) is counted in kilobytes, so on macOS my code used ~2GB of memory when on Linux it used only ~20MB?

And is this a good decision to measure memory used by user program with rusage.Maxrss or there are better approach?

  • 1
    On my Macs, the `getrusage()` man page says: "_ru_maxrss_ the maximum resident set size utilized (**in bytes**)" (emphasis added). – Ken Thomases Jan 25 '20 at 21:57
  • Thank you very much @ken-thomases, i've seen [this](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getrusage.2.html]) and haven't seen local man. It's my fault. – Mavr Huston Jan 25 '20 at 22:16
  • You're welcome. I don't know if that's a difference between macOS and iOS or if it's something that changed over time and that link, being in the unmaintained documentation archive, shows the earlier man page. (And, if it did change over time, it might be just that the man page was incorrect and has been corrected; or, it might be that the behavior of the OS changed and the man page accurately reflects the behavior.) – Ken Thomases Jan 26 '20 at 04:47

2 Answers2

4

On my Macs, the getrusage() man page says: "ru_maxrss the maximum resident set size utilized (in bytes)" (emphasis added). That seems to make sense of your results.

The iOS man page in Apple's legacy documentation archive to which you were referring does say the units are kilobytes. It's not clear if that's due to different behaviors between iOS and macOS or an error in the man page that's since been corrected. It's a shame that Apple doesn't keep maintained man pages online.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

As best I can tell (from testing plus manual pages)

  • On FreeBSD, OpenBSD, & NetBSD maxrss is in KBytes but ixrss is in Bytes.

  • On Macos since at least 10.11(El Capitan) maxrss is in Bytes but ixrss is in KBytes

  • On MacOS X 10.6 ... ?? maxrss and ixrss are in KBytes

  • On Linux (since 2.6.32) maxrss is in KBytes and ixrss is undefined.

ferg
  • 196
  • 1
  • 11