how to free device_vector<int>

孤者浪人 提交于 2019-12-10 10:38:27

问题


I allocated some space using thrust device vector as follows:

thrust::device_vector<int> s(10000000000);

How do i free this space explicitly and correctly?


回答1:


device_vector deallocates the storage associated when it goes out of scope, just like any standard c++ container.

If you'd like to deallocate any Thrust vector's storage manually during its lifetime, you can do so using the following recipe:

// empty the vector
vec.clear();

// deallocate any capacity which may currently be associated with vec
vec.shrink_to_fit();

The swap trick mentioned in Roger Dahl's answer should also work.




回答2:


clear() sets the size of the vector to 0, but may not release the associated memory. The standard way to release the memory with STL is to swap the vector with an empty vector. It should work for Thrust as well.

v.clear();
device_vector<T>().swap(v);


来源:https://stackoverflow.com/questions/11113899/how-to-free-device-vectorint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!