Possible Duplicate:
How to downsize std::vector?
C++ vector::clear
When I call vector<double>::clear() on some vectors with large sizes, I do not see the memory returned to the system in Task Manager / Performance.
Apparently this is because the container expects you will use that same memory again. I am not going to use that memory again, once the task is done, and the memory would be better returned to the system.
Is there a way to ensure the memory returns to the system immediately, other than using a pointer like vector<double> * v / calling delete on v when we want the memory returned to the system?
C++11 introduced
shrink_to_fitmethod that should do it (note that it’s not strictly guaranteed to work, but it’s very likely it will):If you can’t use C++11 features, then you can use following trick, which will clear your vector and deallocate all the memory it uses:
That said, it’s possible that, even though the vector will free whatever memory it allocated, the allocator won’t give the memory back to the operating system. But at least this memory will be available for other parts of your application.