如何使用 C++ 测量块或程序中使用的内存
测量 C++ 程序或 C++ 程序中的块使用的内存的最佳方法是什么。因此,测量代码应该是代码的一部分,并且不应该从外部测量。我知道该任务的难度,因此不必 100% 准确,但至少让我对内存使用情况有一个良好的印象。
What is the best way to measure the memory used by a C++ program or a block in a C++ program. The measurement code should thereby be part of the code and it should not be measured from outside. I know of the difficulty of that task, so it does not have to be 100% accurate but at least give me a good impression of the memory usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除非您愿意将检测直接显式添加到被测代码中,否则在块级别进行测量将很困难(最多)。
我不会从类级别重载
new
和delete
开始尝试执行此操作。相反,我会使用::operator new
和::operator delete
的重载。这基本上是漏斗的尖端(可以这么说)——所有其他动态内存管理最终都归结为调用它们(大多数都是直接调用)。因此,他们通常会尽可能地告诉您整个程序的动态内存使用情况。您需要处理单个类的
new
和delete
重载的主要时间是它们已经重载,因此它们正在管理一个单独的池,并且您关心在给定时间该池的使用量。在这种情况下,您(几乎)需要直接向它们添加工具,以获得给定时间间隔内内存使用量的高水位线之类的信息。Measuring at the block level will be difficult (at best) unless you're willing to explicitly add instrumentation directly to the code under test.
I wouldn't start with overloads of
new
anddelete
at the class level to try to do this. Instead, I'd use overloads of::operator new
and::operator delete
. That's basically the tip of the funnel (so to speak) -- all the other dynamic memory management eventually comes down to calling those (and most do so fairly directly). As such, they will generally do the most to tell you about dynamic memory usage of the program as a whole.The main time you'd need to deal with overloads of
new
anddelete
for an individual class would be if they're already overloaded so they're managing a separate pool, and you care about how much of that pool is in use at a given time. In that case, you'd (just about) need to add instrumentation directly to them, to get something like a high-water mark on their memory usage during a given interval.重载
new
和delete
可能是正确的方法。不仅针对特定类别,而且可能更普遍。但您还需要标记new
和delete
的位置。并进行某种启动/重置或标记,现在您进入该块,现在您退出该块。然后您需要保留历史记录以便能够在事件发生后对其进行监控。Overloading
new
anddelete
is probably the way to go. Not only on a specific class but maybe more general. But you also need to mark where thenew
anddelete
is made. And have some kind of start/reset or something to mark now you enter this block, and now you exit the block. And then you need to keep the history to be able to monitor it after it happened.您想要的是
new
和delete
运算符的类级别覆盖。Class level override of the
new
anddelete
operators are what you want.正如其他人指出的那样,您可以重载 new 和 delete 来测量分配了多少堆。要对堆栈执行相同的操作,如果您喜欢冒险,则必须执行一些 ASM。在x86-64的GCC中获取堆栈位置:
这将把堆栈指针的地址放入x中。将其放在代码周围的几个位置,并比较进入块之前/之后的值。
请注意,由于编译器如何/何时分配内存,这可能需要一些工作才能获得您想要的内容;它并不像听起来那么直观或微不足道。
As others have pointed out, you can overload new and delete to measure how much heap was allocated. To do the same for the stack, if you feel adventurous, you'll have to do some ASM. In GCC in x86-64 to get the stack position:
This will put in x the address of the stack pointer. Put this in a few places around your code and compare the values before/after entering the block.
Please note that this might need some work to get what you want because of how/when the compiler might allocate memory; it is not as intuitive or trivial as it sounds.