0

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

Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
  • 1
    https://stackoverflow.com/questions/7880784/what-is-rss-and-vsz-in-linux-memory-management - according to this question, RSS doesn't show memory that is paged out. Maybe this is the reason it doesn't change? – braindf Nov 04 '19 at 18:55

2 Answers2

1

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 1
    Even then, RSS might not go up that much. RSS is memory that's paged in, but allocating memory doesn't always page it in, and when some memory gets pages in some other memory might get paged out. – Barmar Nov 04 '19 at 19:34
  • 2
    Note that `vector(n)` allocates and **fills** the memory. – Maxim Egorushkin Nov 04 '19 at 20:24
0

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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271