Reputation: 1709
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
Upvotes: 1
Views: 1252
Reputation: 136266
The heap already has some memory allocated and included in RSS, so you are just using that.
Increase your allocation size from 4 * 1024
to, say, 64 * 1024
and observe RSS grow.
Upvotes: 0
Reputation: 123480
No, RSS is not expected to grow after every single allocation.
It's inefficient to keep asking the OS for tiny amounts of memory, so a good allocator will request a larger chunk, and then parcel it out without getting the OS involved.
Additionally, memory is paged in lazily. A large, untouched allocation will not contribute to RSS. (In this particular case, the vector will make sure the memory is initialized, so this is not an issue here, but it could have been if you had allocated it with .reserve(4096)
instead).
This means that you'll instead see memory stay the same for several allocations+initializations in a row, and then suddenly go up. If you keep allocating more data, you'll probably see this effect.
Upvotes: 2