立即使用 2 GB 内存的 C++ 程序:如何找到罪魁祸首? [英] C++ Program immediately using 2 GB of RAM: how to find culprit?
问题描述
我有一个程序,当在初始化时查询时,它会立即使用 > 2 GB 的 RAM.基本上代码是这样的:
I have a program that, when queried on initialisation, is immediately using > 2 GB of RAM. Basically the code is like this:
#include <blah>
int main() {
cout << get_mem_usage() << endl;
//Lots of things happen, but no significant memory usage
return 0;
}
输出:[2013-02-15 18:38:05.865283] 2147.71 Mb
Output: [2013-02-15 18:38:05.865283] 2147.71 Mb
但是,我链接到了许多不同的共享对象文件:我检查了 ldd,我链接到了 58 个库,总 .so 大小为 66 MB.
I am, however, linking to a lot of different shared object files: I checked ldd and I am linking to 58 libraries, with a combined .so size of 66 MB.
我很确定 get_mem_usage 程序正在返回正确的值:这些值与 top 和 massif 输出一致.
I'm pretty sure that the get_mem_usage program is returning the correct values: these agree with top and massif output.
我还应该提到,我正在链接用于高能物理分析的 ROOT 框架.
I should also mention that I am linking against the ROOT framework for High Energy Physics analysis.
我使用的是 Linux x86_64,get_mem_usage() 函数是自产的.
I am using Linux x86_64, and the get_mem_usage() function is homegrown.
如果可能的话,我想减少程序的内存使用.
I would like to reduce the memory usage of the program, if at all possible.
推荐答案
我的第一个猜测:您正在链接的某些库中的静态初始化.
My first guess: static initizations in some of the libraries you are linking.
在 main()
的第一行插入一个长停顿,并查看/proc/
Insert a long pause as the very first line of main()
and look through the /proc/<pid>/ to see where the memory is allocated. For example:
- /proc/12345/task/12345/maps
- /proc/12345/task/12345/smaps
这篇关于立即使用 2 GB 内存的 C++ 程序:如何找到罪魁祸首?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!