c ++程序中进程的内存使用情况

memory usage by a process in c++ program

在我的 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

我觉得当我从 map.but 中删除键值对时它应该释放内存,正如你所看到的,直到我最终删除(释放)映射时它才会释放内存

delete(m_Map);

有人可以解释它在 c++ 中是如何发生的,我查看了 c++ map::emplace,擦除函数 documentation.which 没有提供任何线索..

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

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

所以这并不一定意味着,当您执行 newmalloc/deletefree 时,它会立即导致 RSSVM.