简体   繁体   中英

How to debug high memory consumption

I wrote a program which use OpenCV persistence module to read a .xml.gz file. I use a class to manage these data. In the constructor, it use cv::FileStorage read all the data. All data are in member variables which are either std::vector or cv::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. However, if I use two instances, it will consume about 600MB. If I use three, it will consume about 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.

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.
  2. I use valgrind to check, and there is no memory leak in the program.

Here is the code of this class: 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. The result is: VIRT: 1003552, RES: 693228, SHR: 8440, %MEM: 33.8 (The computer has 2GB memory in total)

Edit2: I use tcmalloc to replace g++'s memory allocator, then the problem disappeared mysteriously.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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