strange values of get rusage.maxrss on macOS and Linux
Asked Answered
P

2

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?

Propriety answered 25/1, 2020 at 21:44 Comment(3)
On my Macs, the getrusage() man page says: "ru_maxrss the maximum resident set size utilized (in bytes)" (emphasis added).Lamphere
Thank you very much @ken-thomases, i've seen this and haven't seen local man. It's my fault.Propriety
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.)Lamphere
L
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.

Lamphere answered 26/1, 2020 at 4:54 Comment(0)
L
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.

Lochner answered 26/1, 2023 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.