简体   繁体   English

在C ++中,即使调用了delete,进程何时保留已分配的内存?

[英]In C++, when does a process retain allocated memory even though delete is called?

I would like to understand what is going on in the GCC runtime in the following situation. 我想了解在以下情况下GCC运行时中发生了什么。

I have a C++ program that allocates many blocks of memory and then deletes them. 我有一个C ++程序,它分配许多内存块然后删除它们。 What's puzzling is that the memory is not being returned to the OS by the GCC runtime. 令人费解的是,GCC运行时没有将内存返回给操作系统。 Instead, it is still being kept by my program, I assume in case I want to allocate similar chunks of memory in the near future. 相反,它仍然由我的程序保存,我假设我想在不久的将来分配类似的内存块。

The following program demonstrates what happens: 以下程序演示了会发生什么:

#include <iostream>
using namespace std;

void pause1()
{
    cout << "press any key and enter to continue";
    char ch;
    cin >> ch;
}

void allocate(int size)
{
    int **array = new int*[size];
    for (int c = 0; c < size; c++) {
        array[c] = new int;
    }
    cout << "after allocation of " << size << endl;
    for (int c = 0; c < size; c++) {
        delete array[c];
    }
    delete [] array;
}

int main() {
    cout << "at start" << endl;
    pause1();
    int size = 1000000;
    for (int i = 0; i < 3; i++) {
        allocate(size);
        cout << "after free" << endl;
        pause1();
        size *= 2;
    }
    return 0;
}

I check the amount of memory held by the process at each pause (when it should not be holding any memory at all) by running "ps -e -o vsz,cmd". 我通过运行“ps -e -o vsz,cmd”检查进程在每次暂停时(当它不应该保留任何内存时)占用的内存量。

The amount held by the process at each pause is the following: 每次暂停时进程持有的金额如下:

2648kb - at start  
 18356kb - after allocating and freeing 1,000,000 ints  
  2780kb - after allocating and freeing 2,000,000 ints  
 65216kb - after allocating and freeing 4,000,000 ints

I'm running on Fedora Core 6 and using GCC 4.1.1. 我正在运行Fedora Core 6并使用GCC 4.1.1。

The memory allocator used by the C library allocates stuff in a variety of ways depending on how big the chunk is. C库使用的内存分配器根据块的大小以各种方式分配内容。 Pages are not always returned to the OS when memory is freed, particularly if you do many small allocations. 释放内存时,页面并不总是返回到操作系统,特别是如果您执行许多小分配。

Memory can only be returned to the OS on a page-by-page basis, not for small allocations. 内存只能逐页返回到操作系统,而不能用于小的分配。

If you really need to know, examine the C library source code and instrument it etc. 如果您真的需要知道,请检查C库源代码并检测它等。

In C++ you can override the allocators for containers to do your own memory management - you can then do whatever you want (eg mmap /dev/zero or whatever) 在C ++中,您可以覆盖容器的分配器以进行自己的内存管理 - 然后您可以随意执行任何操作(例如mmap / dev / zero或其他)

It depends, normally if you are using a Linux box, the underlying malloc will start allocating stuff in the heap and it will grow, however if you free some big inner chunk it won't be able to free anything till the top part of the heap is freed, as the onyl thing it can do is grow or reduce the heap. 这取决于,通常如果你使用Linux盒子,底层的malloc将开始在堆中分配东西并且它会增长,但是如果你释放一些大的内部块,它将无法释放任何东西直到顶部堆被释放,因为它可以做的onyl事情是增加或减少堆。

So, If you allocate a huge chunk, later a smaller chunk and then free your bug chunk, it might happen that they are placed in order and till you free the smaller chunk your process won't be able to reduce the heap size. 因此,如果你分配一个巨大的块,后来一个较小的块然后释放你的bug块,可能会发生它们按顺序放置,直到你释放较小的块,你的进程将无法减少堆大小。

In your current example it might happen that the first frees you do don't are waiting for next allocations and later they can't be freed because you have something allocated on top of it. 在您当前的示例中,可能会发生第一次释放您没有等待下一次分配,之后无法释放它们,因为您已经在其上分配了一些东西。 Or maybe it won't start freeing till a given memory size. 或者也许它不会开始释放直到给定的内存大小。

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

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