我们如何才能找到对象长期和 termsMap使用的内存(在 字节)。我们有图书馆吗?
您应该使用您自己的分配器类型。
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;
我还没有检查这个想法的std :: string,所以如果它是很难实现只使用自己的类fixed_string这只是包装个char [MAX-串lenght。
而当你需要在你的程序中找出内存消耗时,只需从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
得到它。
编辑
对于UncleBens我想澄清我的观点。
就我所了解的ARV问题而言,有必要知道为set :: set和std :: map分配了多少内存,包括为该集合和地图的元素分配的所有内存。所以它不只是sizeof(术语)。
所以我只是建议一个非常简单的分配器。没有进入太多细节,可能是这样的:
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_;
};
这个分配器只计算分配和释放的字节数和真正的分配和释放使用它的成员std_allocator_。我可能需要在gdb下调试它,以便在malloc()和free()上设置一个断点,以确保每次分配和释放实际都通过我的分配器。
如果您将此意见指向某些问题,我将不胜感激,因为我已经在我的Windows,Linux和HP-UX上运行的程序中实现了它,并且我只是简单地询问我的分配器以查找多少内存我的每个容器都使用。
'sizeof'是一个运算符,而不是一个函数。 – 2010-01-29 06:39:51
@Carl Norum:你是对的。谢谢你的提醒! – 2010-01-29 06:41:19
嗯。这看起来没有用,原因有两个:成员是私有的,并且不允许你使用非静态成员变量的大小。有趣! – 2010-01-29 06:43:18