简体   繁体   English

使用 valgrind 时内存泄漏消失

[英]memory leak disappears when using valgrind

We have a complex algorithm, which processes OpenCV images, thereby allocating and deallocating several GB of memory, mostly cv::Mat with about 10MB size each.我们有一个复杂的算法,它处理 OpenCV 图像,从而分配和释放几 GB 的内存,主要是 cv::Mat,每个大约 10MB 大小。 If we run this iteratively unter valgrind (either with --tool=massif or --tool=memcheck) the memory footprint returns to the same value (+-1MB) after each iteration, and no significant memory leak is found.如果我们在 valgrind 下迭代运行(使用 --tool=massif 或 --tool=memcheck),每次迭代后内存占用都会返回相同的值(+-1MB),并且没有发现明显的内存泄漏。 Watching from outside via ps or pmap or from inside via /proc/self/status also shows a maximum footprint of 2.3GB not increasing.通过 ps 或 pmap 从外部观看或通过 /proc/self/status 从内部观看也显示 2.3GB 的最大占用空间没有增加。

If we run the same software without valgrind however, then the memory footprint (checked from outside via ps or pmap or from inside via /proc/self/status) increases with every iteration about several hundred MB, soon reaching 5BG after a few iterations.然而,如果我们在没有 valgrind 的情况下运行相同的软件,那么内存占用(通过 ps 或 pmap 从外部检查或通过 /proc/self/status 从内部检查)随着每次迭代增加大约几百 MB,在几次迭代后很快达到 5BG。

Thus we have something looking like a memory leak, but valgrind is of no help for finding the cause.因此,我们有一些看起来像内存泄漏的东西,但是 valgrind 对查找原因没有帮助。 What could this be?这可能是什么?

(This is C++ under Ubuntu). (这是 Ubuntu 下的 C++)。

Thanks to the comment of @phd I found a solution to my problem: Using tcmalloc reduces the memory footprint dramatically (using 2.5GB instead of 6GB).感谢@phd 的评论,我找到了解决问题的方法:使用 tcmalloc 显着减少了内存占用(使用 2.5GB 而不是 6GB)。 See the attached grafic见附图

RSS memory using different malloc libraries使用不同 malloc 库的 RSS 内存

There seems to be still a slight increase in memory usage with tcmalloc or jemalloc, but at least it's ok for the iteration numbers we usally have. tcmalloc 或 jemalloc 的内存使用量似乎仍然略有增加,但至少对于我们通常拥有的迭代次数来说是可以的。

Still wonder though how malloc can waste so much resources.仍然想知道 malloc 怎么会浪费这么多资源。 I tried to find out with malloc_info(), but with no success.我试图用 malloc_info() 找出答案,但没有成功。 I suspect memory fragmentation and/or multithreading plays a role here.我怀疑内存碎片和/或多线程在这里起作用。

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

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