如何计算哪个函数需要多少字节?

时间:2022-12-04 07:57:41

I have a complex code base in C++. I have run a memory profiler that counts the number of bytes allocated by malloc, this gives me X bytes. Theoretically, my code should return X-Y bytes (Y varies with the input, and ranges from a few KB to a couple of GB, so this is not negligible.)

我在c++中有一个复杂的代码库。我运行了一个内存分析器来计算malloc分配的字节数,这给了我X个字节。从理论上讲,我的代码应该返回X-Y字节(Y随输入而变化,范围从几KB到几GB,所以这是不可忽略的)。

I need to find out which part of my code is asking for the extra bytes. I've tried a few tools, but to no avail: massif, perf, I've even tried gdb breaking on malloc(). I could probably write a wrapper for malloc asking to provide the calling function, but I don't know how to do that.

我需要找出代码的哪一部分需要额外的字节。我已经尝试了一些工具,但都没有效果:massif, perf,我甚至尝试过在malloc()上破坏gdb。我可能会为malloc编写一个包装器,请求提供调用函数,但我不知道如何做到这一点。

Does anyone know a way to find how much memory different parts of the program are asking for?

有人知道这个程序的不同部分需要多少内存吗?

2 个解决方案

#1


2  

If you use a custom allocate function - a wrapper around malloc - you can use the gcc backtrace functions (http://man7.org/linux/man-pages/man3/backtrace.3.html) to find out which functions call malloc with what arguments.

如果您使用一个自定义的分配函数——malloc的包装器——您可以使用gcc回溯函数(http://man7.org/linux/man- pages/man3/backtrace.html)来查找哪些函数调用malloc,使用什么参数。

That'll tell you the functions which are allocating. From there you can probably sort the biggies into domains by hand.

它会告诉你分配的函数。从那里,您可能可以手工将这些大数据分类成域。

This question has good info on the wrapping itself. Create a wrapper function for malloc and free in C

这个问题有关于包装本身的很好的信息。为malloc创建一个包装函数,并在C中免费

Update: This won't catch new/delete allocations but overriding them is even easier than malloc! See here: How to properly replace global new & delete operators + the very important comment on the best answer "Don't forget the other 3 versions: new[], delete[], nothrow"

更新:这不会捕获新的/删除分配,但是覆盖它们比malloc更容易!请看这里:如何正确替换全局new & delete操作符+关于“不要忘记其他三个版本:new[]、delete[]、nothrow”的重要评论

#2


0  

You can make a macro that calls the libc malloc and prints the details of the allocation.

您可以创建一个宏来调用libc malloc并打印分配的详细信息。

#define malloc( sz ) (\
{\
    printf( "Allocating %d Bytes, File %s:%d\n", sz, __FILE__, __LINE__ );\
    void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");\
    printf("malloc\n");\
    void* mem = libc_malloc(sz);\
    mem; // GCC-specific statement-expression \
}

This should ( touch wood ) get called in lieu of the real malloc and spit out the number of bytes allocated and where the allocation occurred. Returning mem like this is GCC-specific though.

应该调用这个(touch wood)来代替真正的malloc,并输出分配的字节数和分配发生的位置。像这样返回mem是gcc特有的。

#1


2  

If you use a custom allocate function - a wrapper around malloc - you can use the gcc backtrace functions (http://man7.org/linux/man-pages/man3/backtrace.3.html) to find out which functions call malloc with what arguments.

如果您使用一个自定义的分配函数——malloc的包装器——您可以使用gcc回溯函数(http://man7.org/linux/man- pages/man3/backtrace.html)来查找哪些函数调用malloc,使用什么参数。

That'll tell you the functions which are allocating. From there you can probably sort the biggies into domains by hand.

它会告诉你分配的函数。从那里,您可能可以手工将这些大数据分类成域。

This question has good info on the wrapping itself. Create a wrapper function for malloc and free in C

这个问题有关于包装本身的很好的信息。为malloc创建一个包装函数,并在C中免费

Update: This won't catch new/delete allocations but overriding them is even easier than malloc! See here: How to properly replace global new & delete operators + the very important comment on the best answer "Don't forget the other 3 versions: new[], delete[], nothrow"

更新:这不会捕获新的/删除分配,但是覆盖它们比malloc更容易!请看这里:如何正确替换全局new & delete操作符+关于“不要忘记其他三个版本:new[]、delete[]、nothrow”的重要评论

#2


0  

You can make a macro that calls the libc malloc and prints the details of the allocation.

您可以创建一个宏来调用libc malloc并打印分配的详细信息。

#define malloc( sz ) (\
{\
    printf( "Allocating %d Bytes, File %s:%d\n", sz, __FILE__, __LINE__ );\
    void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");\
    printf("malloc\n");\
    void* mem = libc_malloc(sz);\
    mem; // GCC-specific statement-expression \
}

This should ( touch wood ) get called in lieu of the real malloc and spit out the number of bytes allocated and where the allocation occurred. Returning mem like this is GCC-specific though.

应该调用这个(touch wood)来代替真正的malloc,并输出分配的字节数和分配发生的位置。像这样返回mem是gcc特有的。