重用变量但进程未释放内存是否被视为内存泄漏?

Is reusing a variable but memory not being released by the process considered a memory leak?

在 Vala 中,我从 Gee 库创建了一个 TreeMultiMap 作为 class 的私有变量。当我使用 tree multi map 并用数据填充它时,进程的内存消耗增加到 14.2 MiB。当我清除仍然是同一个变量的树多图并再次使用它时,但向其添加较少的数据时,进程的内存消耗不会增加,但也不会减少。它保持在 14.2 MiB。

代码如下MultiMapTest.vala

using Gee;

private TreeMultiMap <string, TreeMultiMap<string, string> > rootTree;

public static int main () {
    // Initialize rootTree
    rootTree = new TreeMultiMap<string, TreeMultiMap<string, string> > (null, null);

    // Add data repeatedly to the tree to make the process consume memory
    for (int i = 0; i < 10000; i++) {
        TreeMultiMap<string, string> nestedTree = new TreeMultiMap<string, string> (null, null);
        nestedTree.@set ("Lorem ipsum", "Lorem ipsum");
        rootTree.@set ("Lorem ipsum", nestedTree);
    }

    stdout.printf ("Press ENTER to clear the tree...");
    // Wait for the user to press enter
    var input = stdin.read_line ();

    // Clear the tree
    rootTree.clear ();

    stdout.printf ("Press ENTER to continue and refill the tree with less data...");
    // Wait for the user to press enter
    input = stdin.read_line ();

    // Refill the tree but with much less data
    for (int i = 0; i < 10; i++) {
        TreeMultiMap<string, string> nestedTree = new TreeMultiMap<string, string> (null, null);
        nestedTree.@set ("Lorem ipsum", "Lorem ipsum");
        rootTree.@set ("Lorem ipsum", nestedTree);
    }

    stdout.printf ("Press ENTER to quit...");
    // Wait for the user to press enter
    input = stdin.read_line ();
    return 0;
}

编译为 valac --pkg gee-0.8 -g MultiMapTest.vala

这是否被认为是内存泄漏?如果是这样,是否有任何方法可以正确处理这种情况,例如一旦树多映射被清除,即使它涉及使用其他数据结构,内存也会被释放到 OS?

我使用了 valgrind,但无法检测到任何内存泄漏。我的看法是,一旦为 TreeMultiMap 变量分配了内存,除非该变量超出范围,否则程序将一直分配该内存直到其生命周期结束,而不是将其释放回操作系统。即使TreeMultiMap被清空。

这与 Vala 关系不大,更多的是与 UNIX 程序处理内存的方式有关。

当程序启动时,UNIX 内核为程序本身、堆栈和称为堆的区域分配内存,程序可以在其中动态分配内存。从 UNIX 内核的角度来看,堆是一大块内存,程序可以使用 sbrk.

请求更多

在 C 和大多数其他语言中,您经常需要小块内存。因此,C 标准库有一段代码通过 mallocfree 进行内存分配。当您使用 malloc 分配内存时,它会从堆中的空闲 space 中取出。当你 free 它时,它可以被以后 malloc 重用。如果没有足够的内存,malloc 将调用 sbrk 为程序获取更多内存。无论您释放多少,C 标准库都不会在程序结束之前将内存还给内核。

Valgrind 和 Vala 正在讨论 malloc 没有 free 的内存泄漏。 pstop 查看 sbrk 分配的总内存。

这意味着如果你 malloc 一大块内存,然后 free 它,Valgrind 将显示它已正确释放并且它可供你的程序重用,但内核仍然认为程序正在使用它。