问题描述
是否可以测量在对象被析构函数终止后释放的内存量.我正在尝试检查资源是否得到妥善管理.例如,我编写了一个 LinkedList
的实现并对其进行了测试:
Is it possible to measure amount of memory being released after the object being terminated by destructor. I'm trying to check whether resources are properly managed. For example, I've written an implementation of the LinkedList
and testing it:
int main(int argc, char** argv) {
//check point for initial memory
int64_t init = ??? ;
//next points of measurement
int64_t point_a, point_b;
List<int> list;
list.push_back(5);
{
List<double> l;
l.push_back(-0.12);
l.push_back(1.6);
// ... do something else
// ................
// ................
l.push_back(-4.75);
l.push_back(7.8);
l.print();
point_a = ??? ;// memory state after operations with list
}//calling destructor
point_b = ??? ; // memory state after destruction of the l - object
std::cout << "Initial memory: " << init
<< ", memory in scope: " << point_b
<< ", after destructor: " << (point_b - point_a) << "\n";
return 0;
}//main();
我的问题是:
- 是否有可能实现这一目标?
- 如果是,我应该放什么来代替
???
? - 有没有办法做得更好/不同?
- 这样做有意义吗?
推荐答案
是否有可能实现?是
Is it possible to achieve this? Yes
如果是,我应该放什么而不是 ????使用下述工具之一.
If yes, what should I place instead of ??? ?Use one of the tools described below.
有没有办法做得更好/不同?是的,请参阅 Valgrind 和 MemWatch 如下所述.
Is there a way of doing better/differently ?Yes, see Valgrind and MemWatch described below.
这样做有意义吗?不,如果您的代码有诸如商店吞噬之类的错误,则您的检查也可能有错误.如果可以,最好使用标准工具,或者对于大型项目,创建一个简单的内存审核流程,查找泄漏、记录并重新启动消耗过多内存的任何流程.
Does it make any sense to do this at all ?No, if your code has errors like store gobbling, it is likely that your checks may have errors as well. Best to use a standard tool if you can, or for big projects, create a memory audit process that is simple and looks for leaks, logs it, and restarts any processes that consume too much memory.
检测内存泄漏的两个很棒的工具是 Valgrind 和MemWatch.
Two great tools for detecting memory leaks are Valgrind and MemWatch.
要使用 valgrind
检查代码是否有错误,如果您使用以下方法运行程序:
To use valgrind
to check your code for errors, if you run your program using:
myprog arg1 arg2
然后使用 valgrind
和 --leak-check 选项检查内存泄漏以打开详细的内存泄漏检测器:
Then check for memory leaks using valgrind
with the --leak-check option to turn on the detailed memory leak detector:
valgrind --leak-check=yes myprog arg1 arg2
Valgrind
很棒,因为您可以按原样在已编译的程序上运行它.
Valgrind
is great since you can run it on your compiled program as is.
MemWatch
是另一个有用但不太流行的工具.您可以在此处阅读相关内容.它需要在编译时通过包含memwatch.h"作为每个文件中的最后一个包含来构建到您的程序中.
MemWatch
is another useful tool but less popular. You can read about it here. It needs to be built into your program at compile time by including "memwatch.h" as your last include in each file.
如果在编译时定义了 -DMEMWATCH
,MemWatch
会用自己的包装器替换分配/释放内存的系统调用,以便它可以跟踪内存泄漏.当您运行代码时,它会生成所有分配的详细日志,并报告内存未正确释放的错误.对于在课堂作业中教师要求学生将其包含在他们的项目中的学生来说,这真的很好.学生和助教可以自动运行这些检查,而无需学习如何解释 valgrind 输出.
If -DMEMWATCH
is defined at compile time, MemWatch
replaces system calls that allocate/free memory with its own wrappers so that it can track memory leaks. When you run your code, it will produce a detailed log of all your allocations and report errors were memory was not freed properly. It is really good for students doing class assignments where the instructor asks the students to include it in their projects. Students and TAs can run these checks automatically without needing to learn how to interpret valgrind output.
要使用 MemWatch 构建您的项目,请下载 memwatch.c
和 memwatch.h
此处 来自 GitHub,并使用以下选项进行编译:
To build your project with MemWatch, download memwatch.c
and memwatch.h
here from GitHub, and compile using these options:
gcc -o myProg -DMEMWATCH -DMEMWATCH_STDIO MyProg.c memwatch.c
当您在启用 MemWatch
的情况下运行您的程序时,MemWatch
将报告所有内存管理不善的地方,并将其写入日志,以便您在使用后查看代码运行.如果编译时没有定义MEMWATCH
和MEMWATCH_STDIO
,则MemWatch
不会包含在编译文件中.
When you run your program with MemWatch
enabled, MemWatch
will report all the places where memory is mismanaged, and writes it to a log so you can view it after your code runs. If you do not define MEMWATCH
and MEMWATCH_STDIO
when compiling, MemWatch
is not included in your compiled file.
这篇关于C++ 中的内存测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!