C 内存分析器

发布于 2024-10-09 23:11:14 字数 1539 浏览 0 评论 0原文

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

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

发布评论

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

评论(4

自我难过 2024-10-16 23:11:14

您可能想看看 MemProf

You may want to take a look at MemProf.

椵侞 2024-10-16 23:11:14

如果您只想获取请求大量内存的位置,最简单的方法是修补 malloc 函数或创建一个具有 malloc 调用和跟踪的新库大小来自 malloc 函数。我不是在谈论实现 malloc 调用。 LD_PRELOAD 这个库到您的应用程序。

这是一个示例代码:

/*
 * gcc -shared -fPIC foo.c -ldl -Wl,-init,init_lib -o libfoo.so
 *
 * LD_PRELOAD this library in the environment of the target executable
 *
 */

#include <stdio.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/errno.h>

#ifndef RTLD_NEXT
#define RTLD_NEXT ((void *)-1)
#endif

int init_lib(void)
{
    return 0;
}

void *malloc(size_t size)
{
    /* do required checks on size here */

    return ((void* (*)(size_t))(dlsym(RTLD_NEXT, "malloc")))(size);
}

您可以很好地修改此代码以执行一些其他操作。

If you just want to get the location from where large amount of memory is requested, The easiest way would be to patch malloc function or create a new library having malloc call and track the size form your malloc function. I am not talking about implementing the malloc call. LD_PRELOAD this library to your application.

here is a sample code:

/*
 * gcc -shared -fPIC foo.c -ldl -Wl,-init,init_lib -o libfoo.so
 *
 * LD_PRELOAD this library in the environment of the target executable
 *
 */

#include <stdio.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <sys/errno.h>

#ifndef RTLD_NEXT
#define RTLD_NEXT ((void *)-1)
#endif

int init_lib(void)
{
    return 0;
}

void *malloc(size_t size)
{
    /* do required checks on size here */

    return ((void* (*)(size_t))(dlsym(RTLD_NEXT, "malloc")))(size);
}

You can very well modify this code to do some additional stuff.

勿忘初心 2024-10-16 23:11:14

Massif 确实显示哪些函数导致了内存使用,只要您使用调试信息(-g)编译了程序。它甚至会显示行号。

此信息在 ms_print 输出中的图表下的每个详细快照中以调用树形式给出。详细快照的频率可以通过 Massif 的 --detailed-freq 选项进行控制。有关详细信息,请参阅Massif 手册第 9.2.6 节读取详细的快照信息。

Massif does show you which functions were responsible for the memory usage, as long as you've compiled your program with debugging info (-g). It will even show you the line number.

This information is given as a call tree in each detailed snapshot under the graph in the ms_print output. The frequency of detailed snapshots can be controlled with the --detailed-freq option to massif. See Section 9.2.6 of the Massif manual for details on reading the detailed snapshot information.

猫弦 2024-10-16 23:11:14

正如其他人指出的那样,Massif 提供了详尽的分析信息,但它大大减慢了该过程。

另一个选择是 Google 的 tcmalloc,它有一个嵌入式堆分析器,可以转储带有分配的调用图(请参阅 http://goog-perftools.sourceforge.net/doc/heap_profiler.html),也可以以图形方式可视化。

您可以在运行时使用LD_PRELOAD将其与您的程序链接起来,并且HEAPPROFILE环境变量启用堆分析器。

As others have pointed out Massif gives exhaustive profiling information, but it considerably slows down the process.

Another option is Google's tcmalloc, which has an embedded heap profiler that dumps the call graph with allocations (see http://goog-perftools.sourceforge.net/doc/heap_profiler.html), which can also be visualized graphically.

You can link it at runtime with your program with LD_PRELOAD, and the HEAPPROFILE env variable enables the heap profiler.

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