为什么使用Swap()时STD ::队列不会释放内存
Why std::queue is not releasing memory when swap() is used?
我遇到了std::queue
的一些奇怪的内存问题。当队列(包裹在另一个对象中)由几个线程共享时,即使队列为空并且使用了swap()
的解决方法,也不会发布用于存储元素的内存。我想知道为什么?这是我的队列包装器:
template<typename T>
class thread_safe_queue {
public:
thread_safe_queue() {};
thread_safe_queue(const thread_safe_queue& orig) = delete;
void push(T item){
std::lock_guard<std::mutex> lk(_m);
q.push(item);
_signal.notify_one();
};
T wait_and_pop(){
std::unique_lock<std::mutex> lk(_m);
_signal.wait(lk, [this]{ return !q.empty(); });
auto res = q.front();
q.pop();
return res;
};
T wait_for_and_pop(){
std::unique_lock<std::mutex> lk(_m);
if(_signal.wait_for(lk, std::chrono::seconds(1), [this]{ return !q.empty(); })){
T res = q.front();
q.pop();
return res;
}
else{
return T();
}
};
long unsigned int size(){
std::lock_guard<std::mutex> lk(_m);
return q.size();
}
void clear(){
std::unique_lock<std::mutex> lk(_m);
std::queue<T>().swap(q);
}
private:
std::mutex _m;
std::condition_variable _signal;
std::queue<T> q;
};
样本生产者消费者程序:
void producer_thread(thread_safe_queue<string> * q){
for(int j = 0; j < 5; j++){
std::this_thread::sleep_for(std::chrono::seconds(5));
for(int i = 0; i < 100000; i++){
q->push("ABCDEFG");
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
q->clear();
}
void consumer_thread(thread_safe_queue<string> * q){
while(true){
string a = q->wait_for_and_pop();
if(a == ""){
cout << "Clearing from consumer" << endl;
q->clear();
}
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
int main(int argc, char** argv) {
thread_safe_queue<string> q1;
std::thread t1(&consumer_thread, &q1);
std::thread t2(&producer_thread, &q1);
t2.join();
cout << "Clearing from main" << endl;
q1.clear();
t1.join();
return 0;
}
我尝试从所有三个线程中使用clear()
方法发布内存,但仍未发布内存(根据HTOP和PMAP)。我正在Centos7中运行此程序。
编辑:clear()
如果所有代码在单线程中运行。
处理很少会将内存返回到OS。有时,OS具有可以发送到过程的"我需要地址空间"消息,但是在64位地址空间中,不太可能需要。
测量当前通过OS级内存储器使用分配的过程的内存数量不起作用。
C/C 运行时将内存返回,并将其存储以供以后分配使用,而不是再次对OS进行错误。
相关文章:
- 内存排序在提升::无锁:队列
- 如何通过 malloc 为队列数组分配内存?
- 使用加速进程间创建消息队列 - 内存访问冲突
- 在堆或堆栈上分配的 std::队列的内存
- 将uint32_t添加到不同命名空间中的队列时出现内存泄漏
- 使用队列的非递归回溯:内存不足
- 为什么使用Swap()时STD ::队列不会释放内存
- 可视标准::队列内存消耗导致内存泄漏 - C++
- 关于在这种情况下消息队列与共享内存的适用性或适用性
- C++ 队列内存泄漏
- 从内存转储中查找线程消息队列中的消息
- 堆栈/队列 C++ 中的内存泄漏
- 无法从阵列队列中手动释放内存
- 写入共享内存队列时出现隔离错误
- 是否有一个快速的内存中队列,我可以使用它在达到一定大小时交换项目
- 优先级队列中可能存在内存泄漏
- boost::lockfree::队列似乎没有释放内存,尽管每个集合对象上的析构函数都被调用
- 使用堆栈和队列的Infix到Postfix:无法访问内存c++
- 队列中pop()上的内存浪费
- 与向量相比,C++STL队列内存使用情况