C 退出程序时是否需要释放分配的内存
如果我使用 malloc
在 C 程序中分配了内存,现在我想退出,我是否必须释放分配的内存,或者我可以假设由于我的整个程序终止,它将被释放操作系统?
我在Linux环境下运行。
If I allocated memory in my C program using malloc
and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS?
I run in Linux environment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
任何现代操作系统都会在进程终止后清理所有内容,但依赖于此通常不是一个好的做法。
这取决于您正在编写的程序。如果它只是一个快速运行和终止的命令行工具,您可能不会费心清理。但请注意,正是这种心态导致了守护进程和长时间运行的程序中的内存泄漏。
Any modern operating system will clean up everything after a process terminates, but it's generally not a good practice to rely on this.
It depends on the program you are writing. If it's just a command line tool that runs and terminates quickly, you may not bother cleaning up. But be aware that it is this mindset that causes memory leaks in daemons and long-running programs.
只需退出并让操作系统清理所有内容,这可能是一个很好的设计并且非常高效。 Apple OS X 现在默认执行此操作 :除非应用程序设置了“不要杀我”标志,否则应用程序将在没有通知的情况下被终止。
通常,释放每个内存分配需要花费大量时间。某些内存页可能已被换出,必须读回,以便将它们标记为空闲。内存分配器必须做大量工作来更新空闲内存跟踪数据。所有这些努力都是浪费,因为程序正在退出。
但这必须是通过设计来完成的,而不是因为程序员忘记了分配的内存!
It can be a good design and very efficient to simply exit and allow the operating system to clean everything up. Apple OS X now does this by default: applications are killed without notice unless the application sets a "don't kill me" flag.
Often, freeing every memory allocation takes significant time. Some memory pages may have been swapped out and must be read back in so they can be marked as free. The memory allocator has to do a lot of work updating free memory tracking data. All of this effort is a waste because the program is exiting.
But this must be done by design and not because the programmer has lost track of allocated memory!
在任何情况下,操作系统都会在进程终止时释放它。所以你不需要它,但既然这是一个很好的做法,你为什么不这样做呢? :)
实际上,对于复杂的代码,我不会冒险不发布一些我不确定 100% 没有用的东西,因为程序随后退出。因此,对于任何最小的疑问,只需释放它即可。
In any case it will be freed by the operating system upon process termination. So you don't need it, but since it is a good practice, why don't you do it anyway? :)
Actually with complex code I wouldn't risk to don't release something which I'm not sure at 100% that will be useless because program exits afterwards. So for any minimal doubt just free it.
是的,你可以这样假设。
尽管在不需要内存后立即释放内存是一个很好的做法,即使对于仅运行很短时间的软件也是如此。
Yes you can assume that.
Although it is a good practice to deallocate the memory immediately after it is not needed, even for software that runs for a short time only.
操作系统将回收内存,因此您无需释放它。
大多数程序都会释放内存,因为如果您不释放任何内存,那么您很可能会遇到由这些故意泄漏引起的问题。
The operating system will reclaim the memory so you don't need to free it.
Most programs do free memory though because if you don't free any memory then you are liable to have problems caused by these intentional leaks.
Linux 将在进程终止时释放分配的内存并关闭文件描述符。
Linux will free the allocated memory and close the file descriptors on process termination.
操作系统将回收内存,但是,如果您预计在分配其他内容之前它们会超出范围,那么最好释放它们。但是,您或多或少可以依靠程序的终止来为您处理内存管理。
The OS will reclaim the memory, however it's good practice to free things if you expect they'll run out of scope before you malloc something else. However, you can more or less rely upon the termination of the program to deal with memory management for you.
始终释放分配的内存,因为操作系统将无缘无故地持有更少的内存。这在内存较小的小型操作系统中非常引人注目。
Always free your allocated memory since that the operating system will hold less memory for no reason. It is very noticed in small operating systems that holds small memory size.