Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink std::vector 's size to fit its actual data to save memory usage? vec.swap() doesn't work in MSVC?

Tags:

c++

memory

vector

Actually I have millions of vector objects in my program. In default for each vector, the system will assign more space than it actually needed since those vectors are read-only after they are finish loading.

So I want to shrink their capacity to save memory. a typical way is using vector.swap() method as described in this question:

 std::vector<T> tmp(v);    // copy elements into a temporary vector
           v.swap(tmp);              // swap internal vector data

I have tried this code, but found the .swap() operation doesn't reduce the memory cost actually. (I looked at the Private Working Set size in the task manager to get the process's memory usage)

Why is that? UPDATE" I am using VS 2008, and I have added some lines to output the current capacity during runtime. As seen in below code and its output, the vec.capacity did shrink to 10 after swap, but this doesn't reflect in task manager.

Or maybe just like @Adam Rosenfield said, this change didn't return the freed space to OS? Or I shouldn't use the Task Manager to look up its memory usage? I hope this swap() operation can have the effect as same as delete pointer_to_a_big_vec which will directly free the memory.

My test sample is here:

void vecMemoryTest()
{
  vector<int> vec;

  //make it big!
  for(int i = 0; i <= 100000; i++){vec.push_back(i);}
  for(int i = 0; i <= 100000; i++){vec.push_back(i);}

  cout << "Before .resize() : " << vec.capacity() << endl;

  //OK now I only need the first 10 elements
  vec.resize(10);   

  cout << "After .resize() : " << vec.capacity() << endl;

  //So try to free other spaces
  vector<int> tmp(vec.begin(), vec.end());



  vec.swap(tmp);    // now should free wasted capacity
            // But in fact it doesn't

  cout << "After .swap() : " << vec.capacity() << endl;
}

and the output is:

Before .resize() : 207382 After .resize() : 207382 After .swap() : 10

like image 959
JXITC Avatar asked Dec 13 '22 05:12

JXITC


1 Answers

Check the capacity() of the vector. You will most probably find that the vector in fact reduced its memory usage. What you are seeing is probably malloc() implementation not releasing memory back to the OS.

like image 167
Nikolai Fetissov Avatar answered Jan 22 '23 17:01

Nikolai Fetissov