简体   繁体   English

C ++程序中的进程使用的内存

[英]memory usage by a process in c++ program

In My C++ program I use 在我的C ++程序中,我使用

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

I added 1000000 elements to map and I checked time to time in the loop the memory usage of the process 我添加了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;
        }
    }

then I again print the process RSS memory usage by erasing one by one element from the map 然后我再次通过删除地图中的一个元素来打印过程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;
        }
 }

using this memory usage function 使用此内存使用功能

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;
}

I came up with this result I can't understand really. 我想出了一个我无法理解的结果。

 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

What I feel is it should free the memory when I delete the key,value pairs from the map.but as you can see it will not free the memory untill I delete(free) the map finally 我的感觉是,当我从地图上删除键,值对时,应该释放内存。但是正如您看到的那样,直到我最终删除(释放)地图后,它才会释放内存。

delete(m_Map);

some one can explain how it happens in c++,I looked at c++ map::emplace,erase function documentation.which does not give any clue about this.. 有人可以解释它是如何在c ++中发生的,我看了c ++ map :: emplace,擦除了函数文档。没有给出任何线索。

You can think that processes have two different memory allocators. 您可以认为进程具有两个不同的内存分配器。 First one allocator is used when you request memory from the kernel( sbrk(2) , mmap(2) ). 当您向内核请求内存( sbrk(2)mmap(2) )时,将使用第一个分配器。 Second one is usespace allocator which slices memory you got from first allocator to your requests from the C++ code. 第二个是usespace分配器,它将您从第一个分配器获得的内存切成C ++代码的请求。

When you do new / malloc - you request second allocator for memory(which can use first allocator to request more memory from kernel if it cannot fulfill your request). 当您执行new / malloc ,您请求第二个内存分配器(如果它不能满足您的请求,则可以使用第一个分配器从内核请求更多内存)。 When you do delete / free - you give up memory to second allocator which can(but not must!) return memory to first allocator and to kernel. 当您执行delete / free ,您将内存分配给第二个分配器,该分配器可以(但不是必须!)将内存返回给第一个分配器和内核。

So it doesn't necessary mean, that when you do new , malloc / delete , free it will immediately result in changes of RSS or VM . 因此,这并不一定意味着,当您执行newmalloc / deletefree ,将立即导致RSSVM更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM