简体   繁体   English

在 C++ 中使用 2D 矢量时明显 memory 泄漏

[英]Apparent memory leak while using 2D vector in C++

I decided to use C++ 2D vectors for an applications.我决定将 C++ 二维向量用于应用程序。 While testing some code I encountered a bad alloc error.在测试一些代码时,我遇到了一个bad alloc错误。 I read that this may be caused by memory shortage so I investigated my program's memory use by calling the malloc_stats function at different steps in gdb. I read that this may be caused by memory shortage so I investigated my program's memory use by calling the malloc_stats function at different steps in gdb. While I am not sure I completely understand the output of the function, it seems to indicate memory leaks.虽然我不确定我是否完全理解 function 的 output,但它似乎表明 memory 泄漏。

I tried to sum up the problem in the short code below:我试图在下面的短代码中总结问题:

#include <vector>
#include <iostream>

using namespace std;

vector<vector<double>> get_predictions(){
    vector<vector<double>> predictions;
    for(int i = 0; i < 10; i++){
        vector<double> new_state;
        new_state.push_back(600);
        new_state.push_back(450);
        new_state.push_back(100);
        new_state.push_back(200);
        predictions.push_back(new_state);
    }
    return predictions;
}

int main()
{
    cout << "start" << endl;

    // time loop
    for(int i = 0; i < 10; i++){
        auto predictions = get_predictions();
        // code that uses the latest predictions
    }

    cout << "end" << endl;
    return 0;
}

Now if I call malloc_stats() at the "start" line, the output is similar to this:现在,如果我在“开始”行调用malloc_stats() ,则 output 与此类似:

Arena 0:
system bytes     =     135168
in use bytes     =      74352
Total (incl. mmap):
system bytes     =     135168
in use bytes     =      74352
max mmap regions =          0
max mmap bytes   =          0

At the "end" step, the function gives:在“结束”步骤,function 给出:

Arena 0:
system bytes     =     135168
in use bytes     =      75568
Total (incl. mmap):
system bytes     =     135168
in use bytes     =      75568
max mmap regions =          0
max mmap bytes   =          0

The "In use bytes" field clearly increased. “使用中的字节数”字段明显增加。

Does it really mean that more allocated memory is held?是否真的意味着更多分配的 memory 被持有?

If so, why?如果是这样,为什么? Shouldn't the allocated content be freed once the different vectors go out of scope?一旦不同的向量 go 从 scope 出来,分配的内容不应该被释放吗?

Then, how to avoid such memory issues?那么,如何避免此类 memory 问题呢?

Thanks a lot非常感谢

Due to the suggestions in comments, I abandoned malloc_stats, which does not seem to behave as I expected, and tried out Valgrind on my code.由于评论中的建议,我放弃了 malloc_stats,它的行为似乎不像我预期的那样,并在我的代码上试用了 Valgrind。

Valgrind confirms that there is no leak in the toy example above. Valgrind 确认上面的玩具示例中没有泄漏。

Concerning my main application, it is a program running on ARM.关于我的主要应用程序,它是在 ARM 上运行的程序。 For reasons unknown to me, the base Valgrind implementation gotten via apt install valgrind did not output the lines concerned by memory leaks.由于我不知道的原因,通过apt install valgrind获得的基本 Valgrind 实现没有 output 与 memory 相关的行泄漏。 I had to recompile it from sources available on Valgrind website.我不得不从 Valgrind 网站上的可用资源重新编译它。 The problem must come from architecture-specific particularities?问题必须来自特定于架构的特殊性?

As a side-note, my application also uses CUDA code, which implies many false positives in Valgrind outputs.作为旁注,我的应用程序还使用 CUDA 代码,这意味着 Valgrind 输出中有许多误报。

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

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