查找 c++ 中单个类的内存使用情况

发布于 12-21 15:24 字数 232 浏览 3 评论 0原文

我有一个类 classX 并且想知道该类的所有实例使用了多少内存。每个新实例都是使用 new classX 创建的

有没有办法在不修改源代码的情况下执行此操作(即使用 valgrind 等工具)?

而我可以用什么方法通过修改源代码来做到这一点(我不能修改每个实例创建,但可以修改类本身)。
我能想到的唯一方法是重载 new 运算符(但我不知道如何从那里调用原始的 new 运算符)!

I have a class classX and would like to know how much how much memory all of the instances of this class use. Every new instance is created using new classX

Is there a way to do this without modifying source code (ie using tools like valgrind)?

And what methods can I use to do this by modifying the source code (I can't modify each instance creation, but can modify the class itself).
The only method I can think of is to overload the new operator (but I don't know how to call the original new operator from there)!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅2024-12-28 15:24:19

在类中重载operator new() 非常容易。然后可以使用 :: 调用全局命名空间来指定全局命名空间,如 ::operator new() 中所示。像这样的东西:

class ClassX {
public:
    void* operator new( size_t size )
    {
        // whatever logging you want
        return ::operator new( size );
    }
    void operator delete( void* ptr )
    {
        // whatever logging you want
        ::operator delete( ptr );
    }
};

It's quite easy to overload operator new() in the class. The global one can be then called using :: to specify global namespace as in ::operator new(). Something like this:

class ClassX {
public:
    void* operator new( size_t size )
    {
        // whatever logging you want
        return ::operator new( size );
    }
    void operator delete( void* ptr )
    {
        // whatever logging you want
        ::operator delete( ptr );
    }
};
雨夜星沙2024-12-28 15:24:19

如果您想跟踪堆栈上对象使用的空间,最好在构造函数和析构函数中添加跟踪。像这样的东西应该可以完成工作。
唯一潜在的问题是,您可能还需要跟踪动态分配的成员。

class Tracked
{
   static int space_used;
   static int space_released;
   Tracked() { space_used += sizeof(Tracked); }
   ~Tracked() { space_released += sizeof(Tracked); }
};

int Tracked::space_used = 0;
int Tracked::space_released = 0;

int main()
{
   {
     Tracked t;
     Tracked * t_ptr = new Tracked();
   }

   std::cout<<"used      :"<< Tracked::space_used <<std::endl;
   std::cout<<"released  :"<< Tracked::space_released <<std::endl;
   std::cout<<"live      :"<< Tracked::space_used - Tracked::space_released <<std::endl;
}

If you want to track space used by objects on the stack, you'd be better off adding your tracking in the constructor and destructor. Something like this should do the job.
Only potential issue is that there may be dynamically allocated members that you need to track too.

class Tracked
{
   static int space_used;
   static int space_released;
   Tracked() { space_used += sizeof(Tracked); }
   ~Tracked() { space_released += sizeof(Tracked); }
};

int Tracked::space_used = 0;
int Tracked::space_released = 0;

int main()
{
   {
     Tracked t;
     Tracked * t_ptr = new Tracked();
   }

   std::cout<<"used      :"<< Tracked::space_used <<std::endl;
   std::cout<<"released  :"<< Tracked::space_released <<std::endl;
   std::cout<<"live      :"<< Tracked::space_used - Tracked::space_released <<std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文