I compiled and run the following c++ code test.cpp
on my macOS Sierra.
#include <iostream>
#include <sys/resource.h>
using namespace std;
int main() {
int a = 1;
struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
cout << "Memory usage = " << r_usage.ru_maxrss << endl;
int b = 2;
return 0;
}
Then got:
➜ Desktop git:(master) ✗ ./test
Memory usage = 663552
Then I found the unit of ru_maxrss
is kilobytes here, so the program used 663552 kilobytes? But I only just created an integer.
Another question is: does ru_maxrss
count int b = 2
? or it just counts the memory usage before the line calling ru_maxrss
.
Under OSX, the unit is bytes.
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/getrusage.2.html