Valgrind显示std :: vector分配的时间多于空闲时间,但没有内存泄漏 [英] Valgrind shows std::vector<> times of alloc is more than free, but no memory leak

查看:144
本文介绍了Valgrind显示std :: vector分配的时间多于空闲时间,但没有内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码非常简单:

#include <vector>
int main() {
    std::vector<int> v;
}

然后我用Valgrind构建并运行它:

Then I build and run it with Valgrind:

g++ test.cc && valgrind ./a.out
==8511== Memcheck, a memory error detector
==8511== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8511== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==8511== Command: ./a.out
==8511==
==8511==
==8511== HEAP SUMMARY:
==8511==     in use at exit: 72,704 bytes in 1 blocks
==8511==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==8511==
==8511== LEAK SUMMARY:
==8511==    definitely lost: 0 bytes in 0 blocks
==8511==    indirectly lost: 0 bytes in 0 blocks
==8511==      possibly lost: 0 bytes in 0 blocks
==8511==    still reachable: 72,704 bytes in 1 blocks
==8511==         suppressed: 0 bytes in 0 blocks
==8511== Rerun with --leak-check=full to see details of leaked memory
==8511==
==8511== For counts of detected and suppressed errors, rerun with: -v
==8511== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

问题有两个:

(1)总堆使用量"表示有1个分配和0个空闲空间.我认为1 alloc是因为std :: vector实例在堆中需要一个内存块.没关系;但是为什么在销毁过程中它不释放内存呢?

(1) "total heap usage" indicates that there is 1 alloc and 0 free. I assume that 1 alloc is because a std::vector instance needs a memory chunk in the heap. That's fine; but why doesn't it free the memory during destruction?

(2)而且,如果不释放它,为什么泄漏摘要"中没有内存泄漏?

(2) And, if it doesn't free it, why is there no memory leak in "LEAK SUMMARY"?

(3)顺便说一句,每行前的==8511==是什么意思? (不过,我本可以在手册中查找它.您不必回答这个问题)

(3) By the way, what does ==8511== before each line mean? (I could've look it up in the manual, though. You don't have to answer this)

谢谢!

推荐答案

C ++运行时仍在使用报告的内存.您无需担心. Valgrind的常见问题解答有关此问题的条目:

Reported memory still in use by C++ runtime. You don't need to worry about it. Valgrind's FAQ has an entry regarding this problem:

首先:放松一下,这可能不是错误,而是功能. C ++标准库的许多实现都使用它们自己的内存池分配器.相当多的销毁对象的内存不会立即释放并交还给操作系统,而是保留在池中以备后用.

First of all: relax, it's probably not a bug, but a feature. Many implementations of the C++ standard libraries use their own memory pool allocators. Memory for quite a number of destructed objects is not immediately freed and given back to the OS, but kept in the pool(s) for later re-use.

这篇关于Valgrind显示std :: vector分配的时间多于空闲时间,但没有内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆