c++ - Valgrind 显示 std::vector<> alloc 的次数超过空闲时间,但没有内存泄漏

标签 c++ memory-leaks stl valgrind

代码相当简单:

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

然后我用 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) “total heap usage”表示有1个alloc和0个free。我假设 1 alloc 是因为 std::vector 实例需要堆中的内存块。没关系;但为什么它在销毁期间不释放内存?

(2) 而且,如果它没有释放它,为什么“LEAK SUMMARY”中没有内存泄漏?

(3) 对了,每行前面的==8511==是什么意思? (虽然我可以在手册中查找它。你不必回答这个问题)

谢谢!

最佳答案

报告的内存仍在被 C++ 运行时使用。你不必担心。 Valgrind 的常见问题解答有 an entry关于这个问题:

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.

关于c++ - Valgrind 显示 std::vector<> alloc 的次数超过空闲时间,但没有内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529430/

相关文章:

c++ - stable_partition 如何成为一种自适应算法?

c# - 一元 : Why unary's behavior in c# varies with c/c++

c++ - 为什么我可以使用比分配的内存更多的内存?

c++ - 在qt c++中替换编码字符串的 "="

C++模板抽象继承

c++ - 我们如何以恒定复杂度或 O(1) 交换 2 个数组?

c - 用新字符串替换堆上的字符串

java - 由于 DefaultListableBeanFactory 对象导致 jboss 服务内存泄漏

c++ - 赋值运算符中的内存泄漏

c++ - 为什么我能够在没有 std::qualifier 的情况下调用这个 C++ 标准库函数?