【问题标题】:Why std::queue is not releasing memory when swap() is used?为什么使用 swap() 时 std::queue 不释放内存?
【发布时间】:2016-12-08 16:14:28
【问题描述】:

我遇到了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() 如果所有代码都在单线程中运行,请释放内存。

【问题讨论】:

  • 对象可能会将内存返回给 C 或 C++ 运行时,但这可能不会将内存返回给操作系统,因此 htop 或 pmap 不会注意到任何变化。
  • @MichaelBurr 这对我来说就像是内存泄漏。我尝试使用更大的数据集,该过程愉快地消耗了 2/3GB 的 RAM,并且没有释放它。

标签: c++ multithreading c++11 queue


【解决方案1】:

进程很少将内存返回给操作系统。有时操作系统有一个“我需要地址空间”消息它可以发送给一个进程,但在 64 位地址空间中它不太可能需要。

无法通过操作系统级别的内存使用来测量进程当前分配了多少内存。

C/C++ 运行时获取返回的内存,并将其存储以供以后分配使用,而不是再次对操作系统进行窃听。

【讨论】:

  • 你是说像pmap、top、htop这样的工具无法测量进程使用的内存吗?似乎很奇怪,因为我的印象是这些工具非常可靠。无论如何,如果我在 main(单线程)中运行所有代码,则内存将由 clear() 方法释放。
  • @Hitman_99 他们测量进程向操作系统请求的内存,这与在 C 或 C++ 中分配的内存不同。
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 2018-05-03
  • 2013-01-12
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多