In My C++ program I use
* 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
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
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..