获取Qt对象的大小 [英] Getting the size of a Qt Object

查看:120
本文介绍了获取Qt对象的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Qt和C ++,我需要找出的内存使用的某些Qt类的实例,这通常是使用sizeof,但是在Qt每个类保存一个指针指向另一个类包含实际实现,这个私有实现类的定义在头文件中找不到,但只在源代码中。

I'm using Qt and C++, I need to find out the amount of memory used by instances of certain Qt classes, this is usually done using sizeof, however in Qt each class holds a pointer to an another class containing the actual implementation, the definition of this private implementation class is not found in headers but only in the source-code.

我是Qt的新手,所以可能有一个标准的方法要做到这一点,如果没有你有任何想法来解决这个问题?

I'm new to Qt so may be there's a standard way to do this, if there isn't do you have any ideas to solve this?

推荐答案

,因为为相同类型的不同对象分配的内存量可能甚至不相同(例如,QSomething A可能能够重用缓存中的一些数据,而QSomething B可能必须单独分配等等)。

There's no exact answer to the question, since the amount of memory allocated for different objects of the same type might not even be the same (e.g. QSomething A might be able to reuse some data from a cache whereas QSomething B might have to allocate it separately, etc).

你可以做什么,我想,写一个简单的测试程序启动,分配N的对象的问题,然后去sleep()很长一段时间。当程序正在睡眠时,使用任务管理器(或任何喜欢的工具)来查看进程正在使用多少RAM。然后ctrl-C(或杀死)过程,并用更大的N值运行它,并重复测量。重复这个过程,最终你会知道进程的RAM分配如何随着分配的项目数量的增长,然后你可以做一个小代数来获得每个对象的平均内存成本的概念。

What you could do, I suppose, is write a trivial test program that starts up, allocates N of the object in question, then goes to sleep() for a long time. While the program is sleeping, use Task Manager (or whatever tool you prefer) to see how much RAM the process is using. Then ctrl-C (or kill) the process, and run it again with a larger value for N, and repeat the measurement. Repeat that process and eventually you'll get an idea of how the process's RAM allocation grows with the number of items allocated, and then you can do a little algebra to get a ballpark idea of the average memory cost per object.

(请记住,在启动过程中有一点点内存开销,因此从所有情况中减去N = 0情况下使用的内存,以便测量只是对象的成本而不是环境开销)

(keep in mind that there's a good bit of memory overhead just in starting the process, so subtract the memory used by the N=0 case from all the cases so that you're measuring just the objects' costs and not the environmental overhead)

这篇关于获取Qt对象的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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