When I delete elements in a vector with “erase”, then there is no memory cleared. For example I make a vector of the size 2000. After the creation the program use 1,5 MB memory. When I do the erase call nothing will be cleared. All the elements are gone. But they are still in memory.
For example:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Makes a vector of 2000 items
vector<int> test(200000);
//Pause for test purpose
system("pause");
//erase all elements
test.erase(test.begin(),test.end());
//Pause for test purpose
system("pause");
return false;
}
Size returns 0. But the process still uses 1,5MB of memory.
erasedoesn’t free the memory. After the code in questiontest.capacity() >= 200000is required by standard. Useto reduce vector’s capacity. Please note that this still doesn’t guarantee that the memory use as seen by the rest of the system will drop. The heap of the process cannot be reduced in size in general.