终止程序是否以与free()相同的方式回收内存? [英] Does terminating a program reclaim memory in the same way as free()?

查看:207
本文介绍了终止程序是否以与free()相同的方式回收内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这个答案,它是一个堆栈溢出问题,它说在ac程序的最后释放内存实际上是有害,因为它将无法使用的变量移到系统内存中.

I saw this answer to a stack overflow question that says that freeing memory at the very end of a c program is actually harmful because it moves variables that wouldn't be used again into system memory.

我很困惑,为什么C中的free()方法会执行与操作系统在程序结尾处回收堆不同的任何事情.

I'm confused why the free() method in C would do anything different than the operating system reclaiming the heap at the end of the program.

有人知道free()和终止在内存管理方面是否存在真正的区别,如果有,操作系统如何将这两个区别对待?

Does anyone know if there is a real difference between free() and termination in terms of memory management and if so how the operating system may treat these two differently?

例如

这两个简短程序之间会发生什么不同吗?

would anything different happen between these two short programs?

void main() {
    int* mem = malloc(1);
    return 0;
}

void main() {
    int* mem = malloc(1);
    free(mem);
    return 0;
}

推荐答案

否,与exitabort一样,终止程序不会以与free相同的方式回收内存.使用free会导致某些活动,当操作系统丢弃mallocfree维护的数据时,这些活动最终将无效.

No, terminating a program, as with exit or abort, does not reclaim memory in the same way as free. Using free causes some activity that ultimately has no effect when the operating system discards the data maintained by malloc and free.

exit具有一些复杂性,因为它不会立即终止程序.现在,让我们考虑立即终止程序的效果,然后再考虑以后的复杂性.

exit has some complications, as it does not immediately terminate the program. For now, let’s just consider the effect of immediately terminating the program and consider the complications later.

在通用的多用户操作系统中,当进程终止时,操作系统会释放其用于其他目的的内存. 1 在很大程度上,这仅意味着操作系统系统会执行一些会计操作.

In a general-purpose multi-user operating system, when a process is terminated, the operating system releases the memory it was using for other purposes.1 In large part, this simply means the operating system does some accounting operations.

相反,当您调用free时,程序中的软件将运行,并且它必须查找要释放的内存的大小,然后将有关该内存的信息插入其正在维护的内存池中.这样的分配可能有成千上万.释放所有数据的程序可能必须执行数千次对free的调用.但是,最后,当程序退出时,free产生的所有更改都将消失,因为操作系统将丢弃有关该内存池的所有数据-所有数据都在操作系统执行的内存页中不保存.

In contrast, when you call free, software inside the program runs, and it has to look up the size of the memory you are freeing and then insert information about that memory into the pool of memory it is maintaining. There could be thousands or tens of thousands (or more) of such allocations. A program that frees all its data may have to execute many thousands of calls to free. Yet, in the end, when the program exits, all of the changes produced by free will vanish, as the operating system will discard all the data about that pool of memory—all of the data is in memory pages the operating system does not preserve.

因此,就此而言,您链接到的答案是正确的,因此调用free是浪费的.而且,正如所指出的那样,必须遍历程序中的所有数据结构以获取它们中的指针,以便它们所指向的内存可以被释放,从而导致所有这些数据结构在被换出后都被读入内存中.到磁盘.对于大型程序,可能需要花费大量时间和其他资源.

So, in this regard, the answer you link to is correct, calling free is a waste. And, as it points out, the necessity of going through all the data structures in the program to fetch the pointers in them so the memory they point to can be freed causes all those data structures to be read into memory if they had been swapped out to disk. For large programs, it can take a considerable amount of time and other resources.

另一方面,不清楚是否很容易避免多次调用free.这是因为释放内存并不是终止程序必须清除的唯一内容.程序可能希望将最终数据写入文件或将最终消息发送到网络连接.此外,程序可能没有直接建立所有这些上下文.大多数大型程序都依赖于软件层,并且每个软件包可能都设置了自己的上下文,并且通常没有提供任何方法来告诉其他软件我现在要退出.完成有价值的上下文,但是跳过所有的内存释放."因此,所有所需的清理任务都可能与自由内存任务交织在一起,并且可能没有解开它们的好方法.

On the other hand, it is not clear it is easy to avoid many calls to free. This is because releasing memory is not the only thing a terminating program has to clean up. A program may want to write final data to files or send final messages to network connections. Furthermore, a program may not have established all of this context directly. Most large programs rely on layers of software, and each software package may have set up its own context, and often no way is provided to tell other software "I want to exit now. Finish the valuable context, but skip all the freeing of memory." So all the desired clean-up tasks may be interwined with the free-memory tasks, and there may be no good way to untangle them.

通常应编写软件,以便在程序突然异常中止时不会发生任何可怕的事情(因为这可能是由于断电而不是故意的用户操作而发生的).但是,即使程序可以忍受中止,但正常退出仍然可以带来价值.

Software should generally be written so that nothing terrible happens if a program is suddenly aborted (since this can happen from a loss of power, not just deliberate user action). But even though a program might be able to tolerate an abort, there can still be value in a graceful exit.

返回到exit,调用C exit例程不会立即退出程序.调用出口处理程序(在atexit中注册),刷新流缓冲区,并关闭流.您调用的所有软件库都可能设置了自己的退出处理程序,以便它们可以在程序退出时完成.因此,如果要确保在程序结束时未在程序中使用的库调用free,则必须调用abort,而不是exit.但是通常最好以优雅的方式结束程序,而不是中止程序.调用abort不会调用出口处理程序,刷新流,关闭流或执行其他exit的结束代码-程序调用abort时数据可能会丢失.

Getting back to exit, calling the C exit routine does not exit the program immediately. Exit handlers (registered with atexit) are called, stream buffers are flushed, and streams are closed. Any software libraries you called may have set up their own exit handlers so that they can finish up when the program is exiting. So, if you want to be sure libraries you have used in your program are not calling free when you end the program, you have to call abort, not exit. But it is generally preferred to end a program gracefully, not by aborting. Calling abort will not call exit handlers, flush streams, close streams, or perform other wind-down code that exit does—data can be lost when a program calls abort.

1 释放内存并不意味着它立即可用于其他目的.具体结果取决于内存的每一页.例如:

1 Releasing memory does not mean it is immediately available for other purposes. The specific result of this depends on each page of memory. For example:

  • 如果与其他进程共享内存,则它们仍然需要该内存,因此,释放该进程使用该内存只会减少使用该内存的进程的数量.它不能立即用于任何其他用途.
  • 如果该内存未由任何其他进程使用,但包含从磁盘上的文件映射的数据,则操作系统可能会在需要时将其标记为可用,但暂时不进行处理.这是因为您可能会再次运行同一程序,并且如果数据仍在内存中会很好,那么为什么不把它留在原地以防万一呢?该数据甚至可能由使用相同文件的其他程序使用. (例如,许多程序可能使用相同的共享库.)
  • 如果该内存未被任何其他进程使用,而只是被程序用作工作区,而不是从文件映射而来,则系统可能会将其标记为立即可用并且不包含任何有用的东西.

这篇关于终止程序是否以与free()相同的方式回收内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆