简体   繁体   English

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

[英]When a program terminates what happens to the memory allocated using malloc that is not free'ed?

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. 上面的程序调用malloc来分配一些内存,并且不会调用free来解除分配。 And the program terminates without de-allocating the memory. 程序终止而不取消分配内存。

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? 当程序终止时,分配但不是free的内存会发生什么?

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等。这个代码在终止期间的行为有什么不同吗?

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. 是的,内存由操作系统回收,因此您在技术上不需要free()它。
  2. It's good practice to free everything you malloced anyway. 无论如何,释放你所提出的所有内容都是一种很好的做法。

However, it's important to say why it's good practice to free() everything you've malloced. 但是,重要的是要说明为什么 free()所有你已经编写过的东西都是好的做法。 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. 习惯:如果你习惯于在每次使用malloced时自由释放,那么在程序的生命周期内你不会意外忘记一个内存段。
  2. 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. 对我来说,这是最重要的原因。
  3. Debugging: If we expect that all memory is cleaned up correctly, then spotting memory that's actually leaking is much easier. 调试:如果我们希望所有内存都能正确清理,那么发现实际泄漏的内存要容易得多。

OS will reclaim the memory not free'd up. 操作系统将回收未释放的内存。

But is a good practice to free all memory allocated by malloc 但释放malloc分配的所有内存是一种很好的做法

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. 因此,一个好的做法是程序应该在退出之前清理它所使用的所有资源。

When a process allocates memory dynamically, it borrows block(s) of memory from OS. 当进程动态分配内存时,它会从OS借用内存块。 When a process doesn't need allocated memory it free(s). 当一个进程不需要分配内存时,它就会释放。 Then OS adds those blocks to its free list. 然后OS将这些块添加到其空闲列表中。 The same happens when a process terminates. 当进程终止时也会发生同样的情况。 All the blocks used by the process is reclaimed by the OS. 操作系统回收该过程使用的所有块。

Read memory-management for more info. 阅读内存管理以获取更多信息。

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

暂无
暂无

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

相关问题 exec()更改程序映像后malloc的内存会发生什么? - What happens to malloc'ed memory after exec() changes the program image? 在c语言中使用malloc()分配但未使用free()释放的内存会发生什么情况? - what happens to the memory which is allocated using malloc() and is not freed using free() in c language? 当遇到exit(1)时,malloc的内存会发生什么? - What happens to the malloc'ed memory when exit(1) is encountered? Android NDK:应用程序完成后,使用malloc分配的内存将如何处理? - Android NDK: what happens to memory allocated with malloc when app finishes? LinkedList - 如何释放使用 malloc 分配的 memory - LinkedList - How to free the memory allocated using malloc Valgrind在尝试释放malloc的结构时报告内存错误 - Valgrind reports a memory error when trying to free a malloc'ed struct 由malloc()分配后释放的内存会发生什么情况? - What happens to the memory which is freed after being allocated by malloc()? 可用的malloc内存时程序停止工作 - program stop working when free malloc memory 当试图释放由堆管理器分配的内存时会发生什么,它分配的内容超过了要求? - what happens when tried to free memory allocated by heap manager, which allocates more than asked for? 当您在程序终止之前 malloc 之后没有释放时,会发生什么? - What REALLY happens when you don't free after malloc before program termination?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM