开发者

Return of memory at the termination of a C++ program

When a C++ program terminates, the RAM used during the run gets cleaned and is returned to t开发者_StackOverflow中文版he system, correct?

Question 1) Is this memory returned managed by C++ language features or by the computer hardware itself?

Question 2) Does the memory get returned efficiently/safely, if I terminate a run using ctrl+Z in Unix terminal?


When a C++ program terminates, the RAM used during the run gets cleaned and is returned to the system, correct?

Correct. By System, I hope you mean, Operating System.

Question 1) Is this memory returned managed by C++ language features or by the computer hardware itself?

The returned memory is managed by the operating system (if I correctly understand the question). And before returning to the OS, the memory is managed by the process; in low-level, which means, managed by various language features, such as allocation- deallocation mechanism, constructors, destructors, RAII, etc.

Question 2) Does the memory get returned efficiently/safely, if I terminate a run using ctrl+Z in Unix terminal?

Ctrl+Z suspends the process. It doesn't terminate it. So the memory doesn't get returned to the OS as long as the process is not terminated.

In linux, Ctrl+C terminates the process, then the memory returns to the OS.


  1. Typically both. At least assuming normal termination, destructors will run, which will typically free the memory associated with those objects. Once your program exits, the OS will free all the memory that was owned by that process.

  2. A forced termination often won't run the destructors and such, but any reasonable operating system is going to clean up after a process terminates, whether it did so cleanly or not. There are limits though, so if you've used things like lock files, it probably can't clean those up.


Q: When a C++ program terminates, the RAM used during the run gets cleaned and is returned to the system, correct?

A: Correct. This is true for ANY program, regardless of the language it was written in, and regardless of whether it's Linux, Windows or another OS

Q Is this memory returned managed by C++ language features or by the computer hardware itself?

A: Neither: the operating system is responsible for managing a process's memory.

Q: Does the memory get returned efficiently/safely, if I terminate a run using ctrl+Z in Unix terminal?

A: OS resources (such as memory) are freed. But you can leave files corrupted, IPCs locked, and other Bad Things that are beyond the OS's control if you kill a program gracelessly.

'Hope that helps


Is this memory returned managed by C++ language features or by the computer hardware itself?

Both occur, assuming a proper shutdown (vs. a crash or kill). The standard C/C++ library deallocates any (non-leaked) memory it allocated via OS system calls and ultimately the OS cleans up any leaked memory.

Does the memory get returned efficiently/safely, if I terminate a run using ctrl+Z in Unix terminal?

Ctrl-Z suspends a process on Unix. If you terminate it using kill or kill -9, the memory will be reclaimed (safely / efficiently) by the OS.


They say that dynamically allocated memory is only returned by you, the programmer. For example, a

myclass *obj = new myclass();

always has to have a corresponding

delete obj;

somwehere, or else your program will leak memory, meaning the operating system could think that certain parts of memory are used when in fact they are not - after too many leaks your memory might be used up by false memory entirely and you won't be able to do anything with it.

However, "C++" (effectively meaning "the compiler") takes care of everything that you allocate on the stack, like

myclass obj;

as long as your destructors actually correctly deletes anything which you dynamically create inside that class.

In practice however, if you leak memory, modern operating systems will take care of it and usually clean it up. Usually there's some system in place where the OS will be able to recognize what parts of the memory you actually used, and then simply free everything in there as soon as the application is terminated.

Memory leaks usually only really create problems when your application needs so much memory that it needs to correctly free up some from time to time, or when you continuously leak memory in a loop (like in games), on systems with limited memory (like consoles).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜