Memory Consumption in STL Containers

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?

+8
c ++ memory-management vector stl map
source share
2 answers

For std::vector and std::string capacity, not size, would be a better approximation. For node-based containers ( std::set , etc.), you would like to multiply the number of nodes (approximately the number of elements) times the size of each node. This is only accurate, however, if the allocator does not use an optimized pool allocator for nodes.

If you really want to know how much memory is being used, however, the best strategy would be to replace the global operator new and operator delete and keep track of the actual allocations. Even more accurate will be the replacement of malloc and free . Formally, this is not, but in practice, I have never come across an implementation when it does not work. On the other hand, if you replace malloc and free , you must implement memory management yourself. if you replace operator new and operator delete , you can use malloc and free , which makes it pretty trivial.

Note that each allocation has some fixed overhead. 100,000 with a distribution of 10 bytes each will consume significantly more memory than 10 allocations of 100,000 bytes.

+11
source share

A std::vector<element> usually takes 3 machine words in total + sizeof (element) * capacity() memory. For typical implementations, overhead consists of pointers to the beginning, end, and current vector size. The elements themselves are stored in continuous memory. capacity() usually takes place to double the actual number of elements.

A std::map<element, int> usually takes about 2 machine words in total + 3 machine words per element + [sizeof (element) + sizeof (int)] * num_elements of Memory. For typical implementations, overhead consists of pointers to stored items. The elements themselves are stored in a binary tree with pointers to the parent and two child elements.

With these rules of thumb, all you need to know is the average number of characters per line and the total number of lines to find out the total memory consumption.

+8
source share

All Articles