strange values of get rusage.maxrss on macOS and Linux

1.1k views Asked by At

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?

2

There are 2 answers

0
Ken Thomases On BEST ANSWER

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.

0
ferg On

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.