I'm trying to monitor RSS (Resident Set Size) programmatically in Linux (by parsing /proc/self/stat
) but it seems like RSS does not increase as I allocate memory.
For example, consider the following program that allocates 10 4KB buffers and prints RSS after every allocation.
int main(int argc, char** argv) {
const long pageSizeKb = sysconf(_SC_PAGE_SIZE) / 1024;
cout << "pageSizeKB is " << pageSizeKb << "\n";
std::vector<std::vector<char>> buffers;
for (int i = 0; i < 10; i++) {
buffers.emplace_back(4*1024);
std::string line;
getline(ifstream("/proc/self/stat", ios_base::in), line);
std::vector<string> stats;
boost::split(stats, line, boost::is_any_of(" "));
cout << "allocated " << (i+1)*4 << "KB" << "\tRSS is " << stats[23] << "\n";
}
}
Its output is:
pageSizeKB is 4
allocated 4KB RSS is 53507
allocated 8KB RSS is 53507
allocated 12KB RSS is 53507
allocated 16KB RSS is 53507
allocated 20KB RSS is 53507
allocated 24KB RSS is 53507
allocated 28KB RSS is 53507
allocated 32KB RSS is 53507
allocated 36KB RSS is 53507
allocated 40KB RSS is 53507
Shouldn't RSS increment by one after each allocation (page is 4KB)?
Thanks