如何查找任何对象使用的内存 [英] How to find the memory used by any object

查看:70
本文介绍了如何查找任何对象使用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Help
{
public:
        Help();
        ~Help();

        typedef std::set<string> Terms;
        typedef std::map<string, std::pair<int,Terms> > TermMap;
        typedef std::multimap<int, string, greater<int> > TermsMap;

private:

        TermMap  terms;
        TermsMap    termsMap;
};

我们如何找到对象termtermsMap使用的(以字节为单位)的内存.我们有图书馆吗?

How can we find the memory used (in bytes) by the objects term and termsMap. Do we have any library ?

推荐答案

如果您正在寻找对象的全部内存使用情况,那么在C ++中通常无法解决此问题-尽管我们可以获得实例的大小通过sizeof()本身,该对象始终可以根据需要动态分配内存.

If you are looking for the full memory usage of an object, this can't be solved in general in C++ - while we can get the size of an instance itself via sizeof(), the object can always allocate memory dynamically as needed.

如果您可以找出容器中各个元素的大小,则可以得到下界:

If you can find out how big the individual element in a container are, you can get a lower bound:

size = sizeof(map<type>) + sum_of_element_sizes;

请记住,尽管容器仍可以分配更多内存作为实现细节,对于vectorstring之类的容器,您必须检查

Keep in mind though that the containers can still allocate additional memory as an implementation detail and that for containers like vector and string you have to check for the allocated size.

这篇关于如何查找任何对象使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆