I am working on an application in which I plan to use a pair of STL containers. The application will take certain measures if the memory consumption reaches the threshold value. For this purpose, I need to perform an accurate calculation of how much memory is used by STL containers.
vector<string> StringList map<string, int> mapstring
Here's how I rate memory:
For the size of the StringList , move all the elements of the vector and continue to add row sizes.
string size = sizeof(string) + string.capacity()*sizeof(char)
Then, finally, add sizeof(StringList);
For the size of mapstring, loop over all the keys of the container and continue to add row sizes, and then add int sizes that mapstring.size()*sizeof(int) . Then finally add to this sizeof(mapstring);
I guess the best approach would be to define your own allocator class and keep track of the memory usage inside it, but writing could be nontrivial. Does this rating look good?
c ++ memory-management vector stl map
Frank Q.
source share