dynamically allocated memory after program termination
Asked Answered
F

5

29

When a C/C++ program containing the dynamically allocated memory(using malloc/new) without free/delete calls is terminated, what happens to that dynamically allocated memory? Does the operating system takes back the memory or does that memory becomes unaccessible to other programs?

Forecourse answered 17/7, 2011 at 23:7 Comment(3)
That's called a memory leak. So yes, the memory is unavailable to other programs.Hopple
@Sani: It is called a memory leak though, no, in practice, the memory will not remain unavailable after the process ends.Paper
How many times does this question need to be answered?Outvote
M
46

I don't think that there are any guarantees in the language standard, but modern operating systems which support sparse virtual memory and memory protection (such as MacOS X, Linux, all recent version of Windows, and all currently manufactured phone handsets) automatically clean up after badly-behaved processes (when they terminate) and free the memory for you. The memory remains unavailable, however as long as the program is running.

If you're programming on microcontrollers, on MacOS 9 or earlier, DOS, or Windows 3.x, then you might need to be concerned about memory leaks making memory permanently unavailable to the whole operating system.

Mayworm answered 17/7, 2011 at 23:11 Comment(0)
B
13

Most modern operating systems employ a memory manager, and all userland processes only see so-called virtual memory, which is not related to actual system memory in a way that the program could inspect. This means that programs cannot simply read another process's memory or kernel memory. It also means that the memory manager will completely "free" all memory that has been assigned to a process when that process terminates, so that memory leaks within the program do not usually "affect" the rest of the system (other than perhaps forcing a huge amount of disk swapping and perhaps some "out of memory" behaviour).

This doesn't mean that it's in any way OK to treat memory leaks light-heartedly, it only means that no single program can casually corrupt other processes on modern multi-tasking operating systems (deliberate abuse of administrative privileges notwithstanding, of course).

Balls answered 17/7, 2011 at 23:35 Comment(0)
E
7

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.

Ernestineernesto answered 17/7, 2011 at 23:9 Comment(4)
Usually only after execution is finishedFipple
Do legacy operating system's also free memory?Prussian
@dragonwrenn: Legacy operating systems would be the ones (if any) which do not free memory. Any modern system should release it.Fipple
A modern OS (one written in this millennium, and before) will clean up after your mess. Your mess is in a virtual space that disappears when your program ends. Embedded systems are a different story. Many barely have something that can be called an OS.Deodorize
S
3

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.

Santo answered 18/7, 2011 at 2:46 Comment(0)
F
1

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.

Fipple answered 17/7, 2011 at 23:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.