Search code examples
c++windowsmemory-managementvectorallocation

Can't measure memory allocation caused by std::vector<>::reserve under Windows


Consider the following code snippet:

void OutputMemoryStatus()
  {
  PROCESS_MEMORY_COUNTERS pmc;
  GetProcessMemoryInfo(::GetCurrentProcess(), &pmc, sizeof(pmc));
  std::cout << pmc.WorkingSetSize / (1024.0 * 1024.0) << std::endl;
  }

int main()
  {
  std::vector<int> vec;
  OutputMemoryStatus();
  vec.reserve(100'000'000);
  OutputMemoryStatus();
  std::cout << vec.capacity() << std::endl;
  for (int i = 0; i < 1'000'000; ++i)
    vec.push_back(i);
  OutputMemoryStatus();
  }

The output is the following:

Memory used: 3.11328
Memory used: 3.20703
Capacity: 100000000
Memory used: 7.03516

So it seems that despite the fact that 400 MB of memory were reserved, which is confirmed by the call to capacity(), Windows doesn't consider this memory as allocated, but when we actually fill the vector with 1'000'000 of values, that memory (4MB) is considered as allocated.

How can it be explained?

I'm using Visual Studio 2015, Release configuration, x86 platform.

UPDATE

Now the compiler doesn't know about the actual amount of memory used, but if I input size = 1'000'000, the result is the same as it was before.

  int size;
  std::cin >> size;
  std::vector<int> vec;
  OutputMemoryStatus();
  vec.reserve(100'000'000);
  OutputMemoryStatus();
  std::cout << "Capacity: " << vec.capacity() << std::endl;
  for (int i = 0; i < size; ++i)
    vec.push_back(i);
  OutputMemoryStatus();

Solution

  • The working set is the memory that is actually in physical RAM at any given time, and changes as pages of memory get paged in & out. Memory, especially large allocations, can start off as being paged out. Using them will page them in, but may possibly page something else out in the process.

    PageFileSize is likely a better measurement for the amount of memory committed to the process.

    Also, not sure if Windows does so or not, but overcommitting memory is a thing and can cause the OS to kill your program or others as it sees fit if it so happens that all the processes on the machine need too much memory at the same time.