How can we find the memory used (in bytes) by the objects term and termsMap. Do we have any library ?
You should use your own allocator type.
typedef std::set<string, your_allocator_1_that_can_count_memory_consumption_t> Terms; typedef std::map<string, std::pair<int,Terms>, your_allocator_2_that_can_count_memory_consumption_t> TermMap; typedef std::multimap<int, string, greater<int>, your_allocator_3_that_can_count_memory_consumption_t> TermsMap;
I have not yet checked this idea for std::string so if it is difficult to implement just use your own class fixed_string which just wraps char s[max-string-lenght].
And when you need in your program to find out memory consumption just get it from your_allocator_1_that_can_counts_memory_consumption_t, your_allocator_2_that_can_counts_memory_consumption_t, your_allocator_3_that_can_counts_memory_consumption_t.
Edited
For UncleBens I want to clarify my point.
As far as I understand the question of the ARV it is necessary to know how much memory is allocated for set::set and std::map including all memory allocated for elements of the set and the map. So it is not just sizeof(terms).
So I just suggested a very simple allocator. Without going into too much details it might look like this:
template <class T> class your_allocator_1_that_can_counts_memory_consumption_t { public: // interfaces that are required by the standart private: std::allocator<T> std_allocator_; // here you need to put your variable to count bytes size_t globale_variable_for_allocator_1_to_count_bytes_; };
This allocator just counts number of allocated and deallocated bytes and for real allocation and deallocation use its member std_allocator_. I might need to debug it under gdb in order to set a breakpoint on malloc() and on free() to make sure that every allocation and deallocation actually goes through my allocator.
I would be grateful if you point me at some problems with this idea since I have already implemented it in my program that runs on Windows, Linux and HP-UX and I simply asks my allocators in order to find how much memory each of my containers use.