进程结束后,动态内存是否会释放

程序结束后,动态内存没有free或delete,操作系统会回收吗?

I have a doubt about the role of the operating system in regards to a process lifetime right now. I am using Linux.

Suppose that I have an application that creates a set of objects in the heap using new. During the lifetime of the application I have no need to delete any of those objects except at the exit the application or on an exception before exiting to do the clean-up.

Suppose I don't call delete at the end of the application for all those objects, do usually the OS reclaim/free all the heap allocated to make it available again at the process exits? If the process exit because of an exception or call to return or exit, does this always occurs?

If this is true this means that if I don't call delete there won't be any impact on the OS or on the other applications running on a machine. Right?

I usually use boost shared pointers or use delete but I would like just to clarify this doubt in a OS/Linux context



Short answer: yes, the OS will free this memory.

Most operating systems will free this memory, however it is bad practice to rely upon this behaviour. Some operating systems will not free this memory. For example, embedded systems. For portability, always free all the memory you allocate.

大部分现代操作系统都会释放,但这是个坏习惯。有些操作系统不会,例如嵌入式系统。

C/C++ doesn't have garbage collection feature. If you allocate memory and doesn't free it up, it's of no use while the program execution continues. This is called memory leak. Once the execution finishes, OS takes back this memory and is again available for use.
C/C++没有垃圾回收机制。如果你不释放,程序继续执行时,内存不可用,这就叫内存泄露。一旦执行结束,操作系统会回收,这个内存又可以被使用了。

During the program's execution, you cannot count on the operating reclaiming the memory. That would be a garbage collection feature found in many other languages such as Java and C#. While garbage collected c++ is possible, I don't believe any mainstream implementations use it.

On the other hand, once your program completes its execution, the OS should reclaim the memory used by the program. So during execution the memory remains off-limits, but is accessible again after the program exits.

java和C#有垃圾回收机制。

Modern OSes reclaim all resources of a closed process. Not only memory, but also file handles, etc.
现代操作系统回收关闭进程的所有字段,不仅内存,还有文件句柄等。

No fear, the OS reclaims all the memory. What you need to watch out for is leaving some persistent resources, such as files, in an indeterminate state.

Just FYI my programming language deliberately fails to free memory on exit by default: it's faster that way. However RAII is not permitted. In C++ if you use RAII then you need to take more care.

不必担心,会回收内存。你需要观察,是否遗留了长期资源,比如文件,在不可知状态。

Fast answer is no damage for OS if program don't call destructors of created objects or not freeing memory or other OS handles. Maximum impact is lost part of files which program has written before exiting.

Therefore Herb Satter in their book write about technique that short-lived applications specially doesn't free memory and don't call destructors for maximum speed of execution.

But better that programs normally handles their used OS resources.

最需要关注的是,程序退出时,文件内容的丢失。


On a modern multiprocessing OS that uses Virtual Memory (eg: Windows 7, Linux), it is true that all (OK, not all, but let's not be nit-picky here) resources are process-specific and will be released back to the system when the process terminates.

So does it matter if your program "leaks memory"? Well, that depends on how it does it.

If you allocate a bunch of resources at startup, no it does not really matter if you manually free them at shutdown or just let the OS do it. I'll admit to being a lazy programmer who likes to let the OS handle such things.

However, if you allocate resources in a loop or on demand at runtime for some reason, and don't bother to manage them in some way, then theoretically if you let your program run long enough it will continually "leak" resources until the point comes that there aren't any more left to allocate. This is aBad Thing. Don't do it.

Now there are a lot of platforms that do not behave this way. If you ever end up doing embedded work, you are quite likely to end up on a platform where you have to manage all of your own resources (manually free memory, close file handles, etc.)

现代多进程操作系统,使用虚拟内存。几乎所有的资源是进程相关的,当进程结束时则资源释放。
但是如果你在循环中不停的申请资源而不释放,到达了极限,则会引发问题。
有些系统不会自动回收资源,例如嵌入式开发。
所以,养成申请、释放内存、句柄,肯定是个好习惯。

你可能感兴趣的:(Linux/Unix)