在C中退出程序时是否需要释放分配的内存

时间:2022-09-06 12:09:31

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?

如果我使用malloc在C程序中分配内存,现在我想退出,我是否需要释放分配的内存,或者我是否可以假设由于我的整个程序终止,它将被OS释放?

I run in Linux environment.

我在Linux环境中运行。

8 个解决方案

#1


19  

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.

这取决于你正在编写的程序。如果它只是一个快速运行和终止的命令行工具,那么您可能不需要清理它。但是请注意,正是这种心态导致了守护进程和长时间运行的程序中的内存泄漏。

#2


8  

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.

它可以是一个很好的设计,并且非常高效地退出并允许操作系统清理一切。苹果OS X现在默认这样做:应用程序在没有通知的情况下被杀死,除非应用程序设置了“不要杀我”标志。

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!

但这必须通过设计来实现,而不是因为程序员丢失了分配内存的跟踪!

#3


3  

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.

实际上,有了复杂的代码,我就不会冒险不发布一些我不能百分之百肯定的东西那是没有用的,因为程序之后会退出。所以对于任何微小的怀疑,释放它。

#4


2  

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.

尽管在不需要内存之后立即释放内存是一种很好的实践,即使对于只运行很短时间的软件也是如此。

#5


2  

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.

大多数程序都有空闲内存,因为如果你不释放任何内存,那么你很可能会因为这些故意的泄漏而产生问题。

#6


1  

Linux will free the allocated memory and close the file descriptors on process termination.

Linux将释放分配的内存,并在进程终止时关闭文件描述符。

#7


1  

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.

总是释放分配的内存,因为操作系统会无缘无故地占用更少的内存。在小的操作系统中,内存容量很小,这一点非常值得注意。

#8


0  

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.

操作系统将回收内存,但是如果您期望它们在您malloc其他东西之前超出范围,那么释放它们是很好的实践。但是,您或多或少可以依赖程序的终止来为您处理内存管理。

#1


19  

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.

这取决于你正在编写的程序。如果它只是一个快速运行和终止的命令行工具,那么您可能不需要清理它。但是请注意,正是这种心态导致了守护进程和长时间运行的程序中的内存泄漏。

#2


8  

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.

它可以是一个很好的设计,并且非常高效地退出并允许操作系统清理一切。苹果OS X现在默认这样做:应用程序在没有通知的情况下被杀死,除非应用程序设置了“不要杀我”标志。

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!

但这必须通过设计来实现,而不是因为程序员丢失了分配内存的跟踪!

#3


3  

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.

实际上,有了复杂的代码,我就不会冒险不发布一些我不能百分之百肯定的东西那是没有用的,因为程序之后会退出。所以对于任何微小的怀疑,释放它。

#4


2  

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.

尽管在不需要内存之后立即释放内存是一种很好的实践,即使对于只运行很短时间的软件也是如此。

#5


2  

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.

大多数程序都有空闲内存,因为如果你不释放任何内存,那么你很可能会因为这些故意的泄漏而产生问题。

#6


1  

Linux will free the allocated memory and close the file descriptors on process termination.

Linux将释放分配的内存,并在进程终止时关闭文件描述符。

#7


1  

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.

总是释放分配的内存,因为操作系统会无缘无故地占用更少的内存。在小的操作系统中,内存容量很小,这一点非常值得注意。

#8


0  

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.

操作系统将回收内存,但是如果您期望它们在您malloc其他东西之前超出范围,那么释放它们是很好的实践。但是,您或多或少可以依赖程序的终止来为您处理内存管理。