One technique that is commonly used is to swap the vector you want to empty out with a new and completely empty vector, like this:
// suppose the type of your vectors is vector<int>
vector<int>().swap (myvec[i]);
This is guaranteed to free up all the memory in myvec[i], it's fast and it doesn't allocate any new heap memory or anything.
This is used because the method clear does not offer such a guarantee. If you clear the vector, it always does set its size to zero and destruct all the elements, but it might not (depending on the implementation) actually free the memory.
In C++11, you can do what you want with two function calls: (thanks for the helpful comment)
myvec[i].clear();
myvec[i].shrink_to_fit();
【 在 confusing 的大作中提到: 】
: 不用析构,但是在linux下会hold住这部分内存不返还给操作系统,为的是下次使用的时候速度快一些。
: 除了malloc_trim(0),还不知道用什么方法可以优雅的把内存返还给OS
:
--
FROM 92.40.168.*