Reputation: 1875
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.
Upvotes: 2
Views: 2285
Reputation: 254471
There are two reasons why the process keeps the memory:
std::vector
will only reallocate memory when it grows, not when it shrinks.In C++11, vectors have a shrink_to_fit
member function, which will ask the vector to reduce the amount of allocated memory if possible. There's no guarantee that it will do that, though. In C++03, or if you want to guarantee that the excess memory is freed, you can use the trick of swapping the vector with a newly allocated one:
std::vector<int>(test).swap(test);
or if you want to clear it completely:
std::vector<int>().swap(test);
This trick moves the ownership of the allocated memory to a temporary vector; at the end of the expression, that vector is destroyed, freeing the memory.
Whether or not the memory is released from the process is entirely up to how your library manages the free store. Often, small allocations (up to a few megabytes, perhaps) are handled by a heap - the process requests large blocks of memory from the system, and splits them up into small pieces. The blocks can't be released unless all the small pieces have been freed, and many implementations won't release them even then.
Larger allocations may be requested directly from the system, in which case they will be released once they are freed.
So you may get the effect you expect with code like this:
// A few gigabytes will probably be allocated directly.
// You'll have to reduce this on a 32-bit system, or if there's not enough memory
std::vector<int> test(1000000000)
// Use the "swap trick" to ensure the memory is deallocated.
std::vector<int>().swap(test);
But there is no guarantee that even this will release the memory from the process; the details of memory allocation aren't specified by the standard, and are up to the compiler/library implementation.
Upvotes: 11
Reputation: 4025
When the process terminates, memory is reclaimed in your case. So, there is no need to erase the vector.
If you want to keep using the vector in a loop, then consider using clear()
which clears the contents of the vector.
If the vector is used in an object, then once the object is destroyed, then vector is also destroyed.
But, if the vector contains pointers then you have to explicitly delete them.
Upvotes: 0
Reputation: 18421
You may like to test it this way:
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());
// Call erase or not, but destructor will be called here - freeing all memory
}
//Pause for test purpose
system("pause");
return false;
}
vector.erase
deons't deallocate memory for simple purpose - probably reusability of memory it has allocated, rather than re-allocating for future requests.
Upvotes: 0
Reputation: 72479
erase
doesn't free the memory. After the code in question test.capacity() >= 200000
is required by standard. Use
test.shrink_to_fit(); // in C++11
vector<int>(test).swap(test); // in C++03
to 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.
Upvotes: 8