0

I'm trying to use RSS to estimate the mem usage of my application in linux.

  for (int i = 0; i < 100; ++i) {
    std::cout << "loading map " << i << std::endl;
    {
      process_mem_usage();
      MyApplicaiton app();
      // do things
      process_mem_usage();
    }
  }

the process_mem_usage is basically monitoring the vm_size and rss using this approach How to get memory usage at runtime using C++?

By running this small bench, I only see RSS increase at the first time, and then keep the same.

I was able to claim that there is no mem leak(otherwise RSS should keep increasing). The only explanation is that the process didn't return the memory to the system (even if that memory is currently not used by my application). Is there any way to force the process return the memory to system? (I tried sleeping for a long time but didn't work).

Another example:

  process_mem_usage();
  {
    std::shared_ptr<char> tmp((char *)operator new(500 * 1024 * 1024));
    std::memset(tmp.get(), 1, 500 * 1024 * 1024);
    process_mem_usage();
  }

by running this example I can see RSS increase and then decrease immediately right after I destroy the shared_ptr.

So it's hard to explain what's going on under the hood.

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
  • That's controlled by the heap manager. Memory can be freed by the app but not returned to the OS by the heap manager for various reasons. That's not under app control and there is no general way to force the heap manager to return everything to the OS at any given time. – kaylum Sep 29 '21 at 00:04
  • Yeah - you are trying to undo an optimization. Shoveling 4k pages in and out of a process is expensive, so the language sub-allocator tries to avoid it. – Martin James Sep 29 '21 at 03:52

0 Answers0