memory usage problem.

Hi All,

I am working on optimizing the memory usage of my codes and I found a problem that I do not understand. Please let me know what you think.

Code:

typedef map <pair<string, int>, vector<pair<int, float> > > TDM;

void parsefile (TDM & my_map, string in_file); // parse input file and put it in the data map
float CheckMemUsage (void); // check memory usage by extracting /proc/self/stat info


int main (int argc, char *argv[]) {
TDM my_map;
string infile = "xxx";

printf ("INFO: Memory usage before parse file: %.2fM.\n", CheckMemUsage ());
parsefile (my_map, in_file);
printf ("INFO: Memory usage after parse file: %.2fM.\n", CheckMemUsage ());

for (TDM::iterator it=my_map.begin(); it!=my_map.end(); ++it) {
vector<pair<int, float> > ().swap (it->second);
}
TDM ().swap (my_map);
printf ("INFO: Memory usage after delete map: %.2fM.\n", CheckMemUsage ());


}

The report of this program:

INFO: Memory usage before parse file: 13.46M.
INFO: Memory usage after parse file: 203.53M.
INFO: Memory usage after delete map: 132.59M.


In the function "parsefile", I only use string, vector, pair, map stuffs, without new any memory. So after this program, I assume that only my_map is in memory. After deleting my map, the memory usage should return to before parsing file status (13.46M). But it still reports a 132.59M. What is this 132M for? Is there sth wrong in my program?

Thanks a lot for your time.



Most likely you can't measure memory usage the way you are. Post the contents of CheckMemUsage.
It's more likely that the pages are not being released back to the OS at all when they were allocated as a result of calling new (and not by new[]), but kept around by the application for future use.
I am observing the same behavior, not only with maps.
Topic archived. No new replies allowed.