【问题标题】:memory usage by a process in c++ programc++程序中进程的内存使用情况
【发布时间】:2016-11-29 10:11:22
【问题描述】:

在我使用的 C++ 程序中

* m_Map= new map<int, list<object> >();
delete(m_Map);
m_Map->erase(TxId);

我添加了 1000000 个元素来映射,并在循环中不时检查进程的内存使用情况

for(int x=1;x<=1000000;x++){
    m_Map->emplace(txId, OBject);
    if(x%100000==0) {
        process_mem_usage(vm, rss);
        cout << "after add a key  VM: " << vm << "; RSS: " << rss << endl;
        }
    }

然后我通过从地图中逐一删除元素再次打印进程 RSS 内存使用情况

 for(int x=1;x<=1000000;x++){
      m_Map->erase(x);
      if(x%100000==0) {
           process_mem_usage(vm, rss);
           cout << "after earse a key VM: " << vm << "; RSS: " << rss << endl;
        }
 }

使用这个内存使用函数

void process_mem_usage(double& vm_usage, double& resident_set)
{
   using std::ios_base;
   using std::ifstream;
   using std::string;

   vm_usage     = 0.0;
   resident_set = 0.0;

   // 'file' stat seems to give the most reliable results
   //
   ifstream stat_stream("/proc/self/stat",ios_base::in);

   // dummy vars for leading entries in stat that we don't care about
   //
   string pid, comm, state, ppid, pgrp, session, tty_nr;
   string tpgid, flags, minflt, cminflt, majflt, cmajflt;
   string utime, stime, cutime, cstime, priority, nice;
   string O, itrealvalue, starttime;

   // the two fields we want
   //
   unsigned long vsize;
   long rss;

   stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
               >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
               >> utime >> stime >> cutime >> cstime >> priority >> nice
               >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest

   stat_stream.close();

   long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
   vm_usage     = vsize / 1024.0;
   resident_set = rss * page_size_kb;
}

我想出了这个我真的无法理解的结果。

 Initially VM: 12660; RSS: 1120
after add a key  VM: 28240; RSS: 16960
after add a key  VM: 43816; RSS: 32536
after add a key  VM: 59524; RSS: 48112
after add a key  VM: 75100; RSS: 63688
after add a key  VM: 90676; RSS: 79264
after add a key  VM: 106384; RSS: 95104
after add a key  VM: 121960; RSS: 110680
after add a key  VM: 137672; RSS: 126256
after add a key  VM: 153248; RSS: 141832
after add a key  VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408
after earse a key VM: 168824; RSS: 157408


after destroying the map VM: 12672; RSS: 1536

我觉得当我从地图中删除键值对时它应该释放内存。但是正如你所看到的,直到我最终删除(释放)地图时它才会释放内存

delete(m_Map);

有人可以解释它在 c++ 中是如何发生的,我查看了 c++ map::emplace,erase function documentation.which 没有给出任何线索..

【问题讨论】:

标签: c++ memory-management memory-leaks hashmap emplace


【解决方案1】:

你可以认为进程有两个不同的内存分配器。 当您从内核请求内存时使用第一个分配器(sbrk(2)mmap(2))。 第二个是使用空间分配器,它将您从第一个分配器获得的内存切片到来自 C++ 代码的请求。

当您执行new/malloc 时 - 您请求第二个内存分配器(如果它不能满足您的请求,它可以使用第一个分配器从内核请求更多内存)。当您执行delete/free 时 - 您将内存放弃给第二个分配器,后者可以(但不是必须!)将内存返回给第一个分配器和内核。

所以这并不一定意味着,当您执行new,malloc/delete,free 时,它会立即导致RSSVM 的更改。

【讨论】:

  • 所以当我们像这样运行应用程序几天(或几个月)时。没有免费的地图..当我们向地图添加更多数据几个月并删除其中一些时......就像你提到它是否会在需要时释放已删除对的内存。这种行为如何影响应用程序的性能与内存使用情况
  • 不要求您刚刚释放或删除的内存会立即返回给内核。只有您的内存分配器(及其策略)知道此内存将返回给内核的时刻。如果您必须立即将内存返回给内核 - 您必须在 C++ 中使用直接与内核对话的自定义内存分配器。并通过直接使用您的自定义内存分配器来实例化您的内存。
  • @Noah:当您擦除条目时,这些条目的内存将释放回堆(但不释放到系统)。当您分配一个新条目时,该内存可能会被重用。所以一个长寿的程序应该没有问题。由于可能存在堆碎片(如果您想要 1000 字节,但堆由 3GB 的 999 字节孔和 1 字节块组成),因此生活有点复杂。如果您需要担心这一点,您可能需要一个特殊用途的分配器。
  • 是 C++ 中的 'emplace' 函数,当我们使用 map 或 list 插入新值时使用 'new' 或 'malloc'
  • @Noah 不要求 operator new 由 malloc 函数实现。但通常是(至少 libstdc++ 和 libc++)。
猜你喜欢
  • 1970-01-01
  • 2013-05-30
  • 2015-03-11
  • 2013-05-11
  • 1970-01-01
  • 2010-10-19
  • 2016-05-30
  • 1970-01-01
  • 2020-09-12
相关资源
最近更新 更多