问题描述
我可能会被误导,但是据我所知,操作系统在程序退出或崩溃后会清理内存.
I may be misinformed, but to my knowledge the OS cleans up memory after a program quits or crashes.
如果是这样,在程序结束时释放内存有什么用?我了解,如果忽略了某个程序正在运行并进行分配,则内存可能会变满",但是如果某个程序已经结束,并且OS会分配该程序使用的所有内存,那么手动分配该内存有什么意义呢?
If so, how useful is it to deallocate memory at the end of a program? I understand that if a program is running and deallocating is neglected that memory could become "full", but if a program is already going to end and the OS deallocates all memory used by the program, what is the point of deallocating that memory manually?
推荐答案
从 C中的内存释放问题:
这样说,可能有其他原因导致程序正常终止时应该释放内存:
With this said, there may be other reasons why memory should be freed when a program terminates normally:
- 有责任心的程序员可能希望释放内存,这是一个质量问题.即使不再使用应用程序,在不再需要之后释放内存也是一个好习惯.
- 如果使用工具检测内存泄漏或类似问题,则释放内存将清除此类工具的输出.
- 在一些不太复杂的操作系统中,操作系统可能不会自动回收内存,并且程序有责任在终止之前回收内存.
- 此外,该应用程序的更高版本可以在程序末尾添加代码.如果先前的内存尚未释放,则可能会出现问题.
- The conscientious programmer may want to free memory as a quality issue. It is always a good habit to free memory after it is no longer needed, even if the application is terminating.
- If you use a tool to detect memory leaks or similar problems, then deallocating memory will clean up the output of such tools.
- In some less complex operating systems, the operating system may not reclaim memory automatically, and it may be the program’s responsibility to reclaim memory before terminating.
- Also, a later version of the application could add code toward the end of the program. If the previous memory has not been freed, problems could arise.
这篇关于在C中需要释放内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!