为什么使用Swap()时STD ::队列不会释放内存

Why std::queue is not releasing memory when swap() is used?

本文关键字:队列 内存 释放 STD Swap 为什么      更新时间:2023-10-16

我遇到了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进行错误。