Is std::vector memory freed upon a clear?

C++OptimizationMemoryVector

C++ Problem Overview


Suppose I have a std::vector of structs. What happens to the memory if the vector is clear()'d?

std::vector<myStruct> vecs;
vecs.resize(10000);
vecs.clear();

Will the memory be freed, or still attached to the vecs variable as a reusable buffer?

C++ Solutions


Solution 1 - C++

The memory remains attached to the vector. That isn't just likely either. It's required. In particular, if you were to add elements to the vector again after calling clear(), the vector must not reallocate until you add more elements than the 1000 is it was previously sized to.

If you want to free the memory, the usual is to swap with an empty vector. C++11 also adds a shrink_to_fit member function that's intended to provide roughly the same capability more directly, but it's non-binding (in other words, it's likely to release extra memory, but still not truly required to do so).

Solution 2 - C++

The vector's memory is not guaranteed to be cleared. You cannot safely access the elements after a clear. To make sure the memory is deallocated Scott Meyers advised to do this:

vector<myStruct>().swap( vecs );

Cplusplus.com has the following to say on this:

> Removes all elements from the vector, calling their respective > destructors, leaving the container with a size of 0. > > The vector capacity does not change, and no reallocations happen due > to calling this function. A typical alternative that forces a > reallocation is to use swap:...

Solution 3 - C++

The destructor is called on the objects, but the memory remains allocated.

Solution 4 - C++

No, memory are not freed.

In C++11, you can use the shrink_to_fit method for force the vector to free memory.

http://www.cplusplus.com/reference/vector/vector/

Solution 5 - C++

.resize(0) and .clear() don't release or reallocate allocated memory, they just resize vector to zero size, leaving capacity same.

If we need to clear with freeing (releasing) memory following works:

Try it online!

v = std::vector<T>();

It calls && overload of = operator, which does moving, similar behaviour as with swap() solution.

This assignment technique works when there is && overload of = operator, and it'is guaranteed to be present in all C++11 STD libraries, see here. So if you compile with -std=c++11 (gcc/clang) or /std:c++11 (msvc) switch (or later) my solution is guaranteed to work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser236520View Question on Stackoverflow
Solution 1 - C++Jerry CoffinView Answer on Stackoverflow
Solution 2 - C++Alexander ChertovView Answer on Stackoverflow
Solution 3 - C++Ryan GuthrieView Answer on Stackoverflow
Solution 4 - C++BAKView Answer on Stackoverflow
Solution 5 - C++ArtyView Answer on Stackoverflow