Is leaked memory freed up when the program exits?

C++Memory Leaks

C++ Problem Overview


If I programmed — without knowing it — a memory leak, and the application terminates, is the leaked memory freed?

C++ Solutions


Solution 1 - C++

Yes, a "memory leak" is simply memory that a process no longer has a reference to, and thus can no longer free. The OS still keeps track of all the memory allocated to a process, and will free it when that process terminates.

In the vast majority of cases the OS will free the memory - as is the case with normal "flavors" of Windows, Linux, Solaris, etc. However it is important to note that in specialized environments such as various Real-Time Operating Systems the memory may not be freed when the program is terminated.

Solution 2 - C++

The OS executing your program usually does cleanup memory that is not freed explicitly and handles that are not closed explicitly, but this is not guaranteed by the C++ standard. You may find some embedded device that do not free up your memory leaks.

That being said Windows and all distros of Linux that I've ever seen do free up memory leaks.

You can easily create a huge loop of memory leaks though to test it out yourself. Watch your RAM usage grow and then close your program. You will see that the RAM usage goes back down.


Another consideration when using C++ is that if you aren't deleting your heap allocated memory then your destructors are also not being called. Sometimes you will have other side effects as well if your destructors aren't called.

Solution 3 - C++

Are you running on a desktop OS (Windows, Linux etc.)? If so, yes, in general the system will free any memory associated with the program when the program exits.

Solution 4 - C++

Usually, yes. Some systems support things like shared memory blocks that aren't automatically freed when a program exits though. Most still keep a reference count and delete it when all the programs that opened it exit, but a few don't (e.g., 16-bit Windows had a few types of items that would remain allocated even when nothing referred to them -- though it usually crashed for other reasons before enough of this accumulated to cause a problem...)

Solution 5 - C++

As far as I know, a modern operating system will free this memory once the program terminates.

Solution 6 - C++

Depends on what memory you leaked. Some memory can't be reclaimed by the OS. Most memory on most OSes however will be automatically reclaimed when the process exits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijn CourteauxView Question on Stackoverflow
Solution 1 - C++Justin EthierView Answer on Stackoverflow
Solution 2 - C++Brian R. BondyView Answer on Stackoverflow
Solution 3 - C++VickyView Answer on Stackoverflow
Solution 4 - C++Jerry CoffinView Answer on Stackoverflow
Solution 5 - C++ravibhagwView Answer on Stackoverflow
Solution 6 - C++PuppyView Answer on Stackoverflow