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
go run test.go
page size: 4096
rusage.Maxrss: 2007040
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?
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.