1

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

1
  • 1
    stackoverflow.com/questions/7880784/… - according to this question, RSS doesn't show memory that is paged out. Maybe this is the reason it doesn't change? Commented Nov 4, 2019 at 18:55

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Note that vector<char>(n) allocates and fills the memory.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.