简体   繁体   English

如何调试高内存消耗

[英]How to debug high memory consumption

I wrote a program which use OpenCV persistence module to read a .xml.gz file. 我编写了一个使用OpenCV持久性模块读取.xml.gz文件的程序。 I use a class to manage these data. 我使用一个类来管理这些数据。 In the constructor, it use cv::FileStorage read all the data. 在构造函数中,它使用cv::FileStorage读取所有数据。 All data are in member variables which are either std::vector or cv::Mat . 所有数据都在成员变量中,这些成员变量可以是std::vectorcv::Mat There are no static members or anything shared between each instance of the class. 在类的每个实例之间没有静态成员或任何共享的对象。

Here is the strange thing. 这是奇怪的事情。 If I use only one instance of my class, the program will consume about 50MB memory. 如果仅使用类的一个实例,则该程序将消耗约50MB的内存。 However, if I use two instances, it will consume about 600MB. 但是,如果我使用两个实例,它将消耗约600MB。 If I use three, it will consume about 650MB. 如果我使用三个,它将消耗大约650MB。 If I use four, it will consume about 700MB ... There seems to be a strange line between only one instance and more than one instance. 如果使用四个,则将消耗约700MB的内存。似乎只有一个实例与多个实例之间存在奇怪的界限。

I did some experiments and here are some results: 我做了一些实验,下面是一些结果:

  1. This problem only happens on Linux (Ubuntu 14.04, OpenCV 3.0), not on Windows. 仅在Linux(Ubuntu 14.04,OpenCV 3.0)上而不在Windows上会发生此问题。
  2. I use valgrind to check, and there is no memory leak in the program. 我使用valgrind进行检查,并且程序中没有内存泄漏。

Here is the code of this class: https://github.com/delphifirst/FaceX/blob/master/FaceX/face_x.cpp 这是此类的代码: https : //github.com/delphifirst/FaceX/blob/master/FaceX/face_x.cpp

Pointing out the problem in my code or give me some suggestions for debugging this kind of problems will both be very helpful. 在我的代码中指出问题或为我提供一些调试此类问题的建议都将非常有帮助。

Edit: I follow the suggestions in the comment, and use top -p instead of gnome-system-monitor to check the memory consumption. 编辑:我遵循注释中的建议,并使用top -p而不是gnome-system-monitor来检查内存消耗。 The result is: VIRT: 1003552, RES: 693228, SHR: 8440, %MEM: 33.8 (The computer has 2GB memory in total) 结果是:VIRT:1003552,RES:693228,SHR:8440,%MEM:33.8(计算机总共有2GB内存)

Edit2: I use tcmalloc to replace g++'s memory allocator, then the problem disappeared mysteriously. Edit2:我使用tcmalloc替换g ++的内存分配器,然后问题神秘地消失了。

You can link to tcmalloc (a memory allocation library that will replace the usual memory allocation routines in your program) and use its heap profiler. 您可以链接到tcmalloc(一个内存分配库,它将替换程序中的常规内存分配例程)并使用其堆分析器。 The heap profiler will tell you where your memory consumption is coming from (ie which functions in your program are allocating all that memory). 堆分析器将告诉您内存消耗的来源(即程序中的哪些函数正在分配所有内存)。 That should give you a good starting point for further debugging. 这将为您进行进一步调试提供一个良好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM