dtrosset's point is well put but apparently misunderstood. Add a means to terminate the program so you can perform a clean analysis. This can be something as simple as adding a signal handler for SIGUSR1, for example, that terminates the program at a point in time you decide. There are a variety of methods at your disposal depending on your OS.
There's a big difference between an application which never exits (embedded, daemons, etc) and one that cannot be exited. The prior is normal, the latter is bad design.
If anything, that application can be forcibly aborted (SIGKILL on *nix, terminate on win32) and you'd get your analysis. That method doesn't give your application the opportunity to clean up before it's destroyed so there will be very likely be retained memory reported.
Profiling is intrusive, so you don't want to deploy the app with the profiler attached, anyway. Therefore, include some #ifdef PROFILE_MODE-code that exits the app after an appropriate amount of time. Compile with -DPROFLILE_MODE, profile. Deploy without PROFILE_MODE.
Modify your program slightly so that you can request a Valgrind leak check at any point - when the command to do that is recieved, your program should use VALGRIND_DO_LEAK_CHECK from memcheck.h (this will have no effect if the program isn't running under Valgrind).
您可以使用 GNU gprof,但它也有一个问题,即它需要退出程序。 您可以通过调用 gprof 的内部函数来克服这个问题。 (见下文)这可能是一个真正的“肮脏”黑客,具体取决于 gcc 的版本和,和,和,...但它有效。
#include "sys/gmon.h"
extern "C" //the internal functions and vars of gprof
{
void moncontrol (int mode);
void monstartup (unsigned long lowpc, unsigned long highpc);
void _mcleanup (void);
extern void _start(void), etext(void);
extern int __libc_enable_secure;
}
// call this whenever you want to write profiling information to file
void WriteProfilingInformation(char* Name)
{
setenv("GMON_OUT_PREFIX",Name,1); // set file name
int old = __libc_enable_secure; // save old value
__libc_enable_secure = 0; // has to be zero to change profile file name
_mcleanup();
__libc_enable_secure = old; // reset to old value
monstartup(lowpc, highpc); // restart profiler
moncontrol(1); // enable profiler
}
You can use GNU gprof, but it has also the problem that it requires an exit of the program. You can overcom this by calling internal functions of gprof. (see below) It may be a real "dirty" hack, depending on the version of gcc and, and, and,... but it works.
#include "sys/gmon.h"
extern "C" //the internal functions and vars of gprof
{
void moncontrol (int mode);
void monstartup (unsigned long lowpc, unsigned long highpc);
void _mcleanup (void);
extern void _start(void), etext(void);
extern int __libc_enable_secure;
}
// call this whenever you want to write profiling information to file
void WriteProfilingInformation(char* Name)
{
setenv("GMON_OUT_PREFIX",Name,1); // set file name
int old = __libc_enable_secure; // save old value
__libc_enable_secure = 0; // has to be zero to change profile file name
_mcleanup();
__libc_enable_secure = old; // reset to old value
monstartup(lowpc, highpc); // restart profiler
moncontrol(1); // enable profiler
}
Some tools allow you to force a memory analysis at any point during the program's execution. This method is not as reliable as checking on exit, but it gives you a starting point.
您的问题看起来就像您在寻找top。它很好地显示(除其他外)所有正在运行的进程的当前内存消耗。 (仅限终端中的一页。)在 Linux 上,按“M”按内存使用情况排序。手册页显示了更多用于排序和过滤的选项。
Your question reads as if you were looking for top. It nicely displays (among other things) the current memory consumption of all running processes. (Limited to one page in the terminal.) On Linux, hit “M” to sort by memory usage. The man page shows more options for sorting and filtering.
我已经使用 Rational purify API 来检查增量泄漏。没用过linux下的API。我在 Valgrind 用户手册中找到了 VALGRIND_DO_LEAK_CHECK 选项,我认为这足以满足您的需求要求。
I have used rational purify API's to check incremental leaks. Haven't used the API's in linux. I found the VALGRIND_DO_LEAK_CHECK option in Valgrind User Manual, I think this would suffice your requirement.
For windows, DebugDiag does that. Generates a report in the end with probable memory leaks. Also has memory pressure analysis. And it's available for free @ microsoft. Download it from here
您需要堆栈截图。使用 pstack 或 lsstack,或者在调试器或 IDE 下运行它并随机暂停 (Ctrl-C)。它不会告诉您有关内存泄漏的信息,但它会让您很好地了解时间的使用方式和原因。
如果由于内存泄漏而使用时间,您将看到很大一部分样本以内存管理例程结束。如果它们位于 malloc 或 new 中,则在堆栈的较高层,您将看到正在分配哪些对象以及原因,并且您可以考虑如何减少分配的频率。
You need stackshots. Either use pstack or lsstack, or just run it under a debugger or IDE and pause it (Ctrl-C) at random. It will not tell you about memory leaks, but it will give you a good idea of how the time is being used and why.
If time is being used because of memory leaks, you will see a good percent of samples ending in memory management routines. If they are in malloc or new, higher up the stack you will see what objects are being allocated and why, and you can consider how to do that less often.
发布评论
评论(13)
我会考虑添加一个从程序中优雅退出的功能。
I'd consider adding a graceful exit from the program.
dtrosset 的观点说得很好,但显然被误解了。添加一个手段来终止程序,以便您可以执行干净的分析。这可以很简单,例如为 SIGUSR1 添加信号处理程序,在您决定的时间点终止程序。根据您的操作系统,您可以使用多种方法。
永不退出的应用程序(嵌入式、守护进程等)和无法退出的应用程序之间存在很大差异。前者是正常的,后者是糟糕的设计。
如果有的话,该应用程序可以被强制中止(*nix 上的 SIGKILL,win32 上终止),然后您就会得到分析。该方法不会让您的应用程序有机会在销毁之前进行清理,因此很可能会报告保留内存。
dtrosset's point is well put but apparently misunderstood. Add a means to terminate the program so you can perform a clean analysis. This can be something as simple as adding a signal handler for SIGUSR1, for example, that terminates the program at a point in time you decide. There are a variety of methods at your disposal depending on your OS.
There's a big difference between an application which never exits (embedded, daemons, etc) and one that cannot be exited. The prior is normal, the latter is bad design.
If anything, that application can be forcibly aborted (SIGKILL on *nix, terminate on win32) and you'd get your analysis. That method doesn't give your application the opportunity to clean up before it's destroyed so there will be very likely be retained memory reported.
分析是侵入性的,因此您无论如何都不想部署带有分析器的应用程序。因此,请包含一些在适当时间后退出应用程序的
#ifdef PROFILE_MODE
代码。使用 -DPROFLILE_MODE、配置文件进行编译。不使用 PROFILE_MODE 进行部署。Profiling is intrusive, so you don't want to deploy the app with the profiler attached, anyway. Therefore, include some
#ifdef PROFILE_MODE
-code that exits the app after an appropriate amount of time. Compile with -DPROFLILE_MODE, profile. Deploy without PROFILE_MODE.稍微修改您的程序,以便您可以随时请求进行 Valgrind 泄漏检查 - 当收到执行此操作的命令时,您的程序应使用
memcheck.h
中的VALGRIND_DO_LEAK_CHECK
(如果程序不在 Valgrind 下运行,这将不起作用)。Modify your program slightly so that you can request a Valgrind leak check at any point - when the command to do that is recieved, your program should use
VALGRIND_DO_LEAK_CHECK
frommemcheck.h
(this will have no effect if the program isn't running under Valgrind).您可以使用 GNU gprof,但它也有一个问题,即它需要退出程序。
您可以通过调用 gprof 的内部函数来克服这个问题。 (见下文)这可能是一个真正的“肮脏”黑客,具体取决于 gcc 的版本和,和,和,...但它有效。
You can use GNU gprof, but it has also the problem that it requires an exit of the program.
You can overcom this by calling internal functions of gprof. (see below) It may be a real "dirty" hack, depending on the version of gcc and, and, and,... but it works.
Rational Purify 可以做到这一点,至少在 Windows 上是这样。 似乎有linux版本,但我不知道如果它能做到同样的事情的话。
Rational Purify can do that, at least on windows. There seem to be a linux version but I don't know if it can do the same.
有些工具允许您在程序执行期间的任何时候强制进行内存分析。此方法不如检查退出那么可靠,但它为您提供了一个起点。
以下是使用 LeakDiag 的Windows 示例。
Some tools allow you to force a memory analysis at any point during the program's execution. This method is not as reliable as checking on exit, but it gives you a starting point.
Here's a Windows example using LeakDiag.
你尝试过 GNU Gprof 吗?
请注意,在本文档中,“cc”和“gcc”可以互换。 (“cc”被假定为“gcc”的别名。)
http://www.cs.utah.edu/dept/旧/texinfo/as/gprof_toc.html
Have you tried GNU Gprof?
Note that in this document, "cc" and "gcc" are interchangeable. ("cc" is assumed as an alias for "gcc.")
http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html
您的问题看起来就像您在寻找
top
。它很好地显示(除其他外)所有正在运行的进程的当前内存消耗。 (仅限终端中的一页。)在 Linux 上,按“M”按内存使用情况排序。手册页显示了更多用于排序和过滤的选项。Your question reads as if you were looking for
top
. It nicely displays (among other things) the current memory consumption of all running processes. (Limited to one page in the terminal.) On Linux, hit “M” to sort by memory usage. The man page shows more options for sorting and filtering.我已经使用 Rational purify API 来检查增量泄漏。没用过linux下的API。我在 Valgrind 用户手册中找到了 VALGRIND_DO_LEAK_CHECK 选项,我认为这足以满足您的需求要求。
I have used rational purify API's to check incremental leaks. Haven't used the API's in linux. I found the VALGRIND_DO_LEAK_CHECK option in Valgrind User Manual, I think this would suffice your requirement.
对于 Windows,DebugDiag 可以做到这一点。
最后生成一份可能存在内存泄漏的报告。
还有内存压力分析。
并且可以免费使用@microsoft。从此处下载
For windows, DebugDiag does that.
Generates a report in the end with probable memory leaks.
Also has memory pressure analysis.
And it's available for free @ microsoft. Download it from here
您需要堆栈截图。使用 pstack 或 lsstack,或者在调试器或 IDE 下运行它并随机暂停 (Ctrl-C)。它不会告诉您有关内存泄漏的信息,但它会让您很好地了解时间的使用方式和原因。
如果由于内存泄漏而使用时间,您将看到很大一部分样本以内存管理例程结束。如果它们位于
malloc
或new
中,则在堆栈的较高层,您将看到正在分配哪些对象以及原因,并且您可以考虑如何减少分配的频率。You need stackshots. Either use pstack or lsstack, or just run it under a debugger or IDE and pause it (Ctrl-C) at random. It will not tell you about memory leaks, but it will give you a good idea of how the time is being used and why.
If time is being used because of memory leaks, you will see a good percent of samples ending in memory management routines. If they are in
malloc
ornew
, higher up the stack you will see what objects are being allocated and why, and you can consider how to do that less often.分析内存泄漏的程序工作是基于检测操作系统而不是程序释放的内存。
Work of program that profiling memory leaks is based on detecting memory that was freed by OS not by program.