当一个程序终止使用不是免费的malloc分配的内存时会发生什么?

时间:2023-01-14 21:19:28

Say I have the following program

说我有以下程序

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    int * i;

    if ((i = malloc(sizeof(int) * 100)) == NULL) {
        printf("EROOR: unable to allocate memory \n");
        return -1;
    }

    /* memory is allocated successfully */

    /* memory is not free'ed but program terminates */
    // free(i);

    return 0;
}

The above program calls malloc to allocate some memory and does not call free to de-allocate it. And the program terminates without de-allocating the memory.

上面的程序调用malloc来分配一些内存,并且不调用free来解除分配。程序终止而不取消分配内存。

Valgrind clearly detects a memory leak.

Valgrind清楚地发现了内存泄漏。

<snap>
==14209== HEAP SUMMARY:
==14209==     in use at exit: 400 bytes in 1 blocks
==14209==   total heap usage: 1 allocs, 0 frees, 400 bytes allocated
==14209== 
<sanp>
==14209== LEAK SUMMARY:
==14209==    definitely lost: 400 bytes in 1 blocks
==14209==    indirectly lost: 0 bytes in 0 blocks
==14209==      possibly lost: 0 bytes in 0 blocks
==14209==    still reachable: 0 bytes in 0 blocks
==14209==         suppressed: 0 bytes in 0 blocks
==14209== 
==14209== For counts of detected and suppressed errors, rerun with: -v
==14209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Question:

题:

When the program terminates, what happens to the memory that was allocated but not free'd?

当程序终止时,分配但不是免费的内存会发生什么?

Update: Consider that this code is being executed on different operation system - say windows, linux, solarix, macos, etc. Is there any difference in the behavior of this code during its termination?

更新:考虑这个代码是在不同的操作系统上执行的 - 比如windows,linux,solarix,macos等。这个代码在终止期间的行为有什么不同吗?

5 个解决方案

#1


12  

The other answers tell you the two important things:

其他答案告诉你两个重要的事情:

  1. Yes, the memory is reclaimed by the OS, so you don't technically need to free() it.
  2. 是的,内存由操作系统回收,因此您在技术上不需要释放()它。
  3. It's good practice to free everything you malloced anyway.
  4. 无论如何,释放你所提出的所有内容都是一种很好的做法。

However, it's important to say why it's good practice to free() everything you've malloced. In my view:

但是,重要的是要说明为什么释放()所有你已经过的游戏都是好的做法。在我看来:

  1. Habit: If you get in the habit of freeing whenever you've malloced, you won't accidentally forget when a memory segment isn't around for the lifetime of the program.
  2. 习惯:如果你习惯于每当你使用malloced时都要释放,你就不会忘记在程序的生命周期内何时没有内存段。
  3. Maintainability: If someone comes along to refactor your program so that a segment of memory is no longer around for the lifetime of the program, the presence of cleanup code in the original will mean that it's very likely that the refactored version also includes the cleanup code. For me, this is the most important reason.
  4. 可维护性:如果有人来重构您的程序,以便在程序的生命周期内不再存在一段内存,原始清单代码的存在将意味着重构版本很可能还包括清理代码。对我来说,这是最重要的原因。
  5. Debugging: If we expect that all memory is cleaned up correctly, then spotting memory that's actually leaking is much easier.
  6. 调试:如果我们期望所有内存都被正确清理,那么发现实际泄漏的内存要容易得多。

#2


7  

O.S. will reclaim the memory not free'd up.

O.S.将收回未释放的内存。

But is a good practice to free all memory allocated by malloc

但是释放malloc分配的所有内存是一个很好的做法

#3


4  

The memory is reclaimed by the Operating system once your program exits.
The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory.

程序退出后,操作系统将回收内存。操作系统不了解您的程序泄漏了内存,它只是将内存分配给程序进行运行,一旦程序退出就回收内存。

However, other resources like file descriptors may/may not be recalimed by the OS causing a resource leak.

但是,操作系统可能会/可能不会重新调整文件描述符等其他资源,从而导致资源泄漏。

So it is a good practice that a program should cleanup all the resource it utilized before exiting.

因此,一个好的做法是程序应该在退出之前清理它所使用的所有资源。

#4


1  

When a process allocates memory dynamically, it borrows block(s) of memory from OS. When a process doesn't need allocated memory it free(s). Then OS adds those blocks to its free list. The same happens when a process terminates. All the blocks used by the process is reclaimed by the OS.

当进程动态分配内存时,它会从OS借用内存块。当一个进程不需要分配内存时,它就会释放。然后OS将这些块添加到其空闲列表中。当进程终止时也会发生同样的情况。操作系统回收该过程使用的所有块。

Read memory-management for more info.

阅读内存管理以获取更多信息。

#5


0  

More importantly, FREE ensures the sanity of the memory/buffers allocated by you, and hence forth exsisting a good checkpoint for curbing/catching up heap corruption.

更重要的是,FREE确保您分配的内存/缓冲区的健全性,从而存在一个用于抑制/捕获堆损坏的良好检查点。

#1


12  

The other answers tell you the two important things:

其他答案告诉你两个重要的事情:

  1. Yes, the memory is reclaimed by the OS, so you don't technically need to free() it.
  2. 是的,内存由操作系统回收,因此您在技术上不需要释放()它。
  3. It's good practice to free everything you malloced anyway.
  4. 无论如何,释放你所提出的所有内容都是一种很好的做法。

However, it's important to say why it's good practice to free() everything you've malloced. In my view:

但是,重要的是要说明为什么释放()所有你已经过的游戏都是好的做法。在我看来:

  1. Habit: If you get in the habit of freeing whenever you've malloced, you won't accidentally forget when a memory segment isn't around for the lifetime of the program.
  2. 习惯:如果你习惯于每当你使用malloced时都要释放,你就不会忘记在程序的生命周期内何时没有内存段。
  3. Maintainability: If someone comes along to refactor your program so that a segment of memory is no longer around for the lifetime of the program, the presence of cleanup code in the original will mean that it's very likely that the refactored version also includes the cleanup code. For me, this is the most important reason.
  4. 可维护性:如果有人来重构您的程序,以便在程序的生命周期内不再存在一段内存,原始清单代码的存在将意味着重构版本很可能还包括清理代码。对我来说,这是最重要的原因。
  5. Debugging: If we expect that all memory is cleaned up correctly, then spotting memory that's actually leaking is much easier.
  6. 调试:如果我们期望所有内存都被正确清理,那么发现实际泄漏的内存要容易得多。

#2


7  

O.S. will reclaim the memory not free'd up.

O.S.将收回未释放的内存。

But is a good practice to free all memory allocated by malloc

但是释放malloc分配的所有内存是一个很好的做法

#3


4  

The memory is reclaimed by the Operating system once your program exits.
The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory.

程序退出后,操作系统将回收内存。操作系统不了解您的程序泄漏了内存,它只是将内存分配给程序进行运行,一旦程序退出就回收内存。

However, other resources like file descriptors may/may not be recalimed by the OS causing a resource leak.

但是,操作系统可能会/可能不会重新调整文件描述符等其他资源,从而导致资源泄漏。

So it is a good practice that a program should cleanup all the resource it utilized before exiting.

因此,一个好的做法是程序应该在退出之前清理它所使用的所有资源。

#4


1  

When a process allocates memory dynamically, it borrows block(s) of memory from OS. When a process doesn't need allocated memory it free(s). Then OS adds those blocks to its free list. The same happens when a process terminates. All the blocks used by the process is reclaimed by the OS.

当进程动态分配内存时,它会从OS借用内存块。当一个进程不需要分配内存时,它就会释放。然后OS将这些块添加到其空闲列表中。当进程终止时也会发生同样的情况。操作系统回收该过程使用的所有块。

Read memory-management for more info.

阅读内存管理以获取更多信息。

#5


0  

More importantly, FREE ensures the sanity of the memory/buffers allocated by you, and hence forth exsisting a good checkpoint for curbing/catching up heap corruption.

更重要的是,FREE确保您分配的内存/缓冲区的健全性,从而存在一个用于抑制/捕获堆损坏的良好检查点。