Is leaked memory freed up when the program exits?

39,460

Solution 1

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

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

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

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

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

Share:
39,460

Related videos on Youtube

Martijn Courteaux
Author by

Martijn Courteaux

I'm writing Java, C/C++ and some Objective-C. I started programming in 2007 (when I was 11). Right now, I'm working on my magnum opus: an iOS, Android, OS X, Linux, Windows game to be released soon on all relevant stores. The game is written in C++ using SDL and OpenGL. A couple of seeds for my name (for java.util.Random, radix 26): 4611686047252874006 -9223372008029289706 -4611685989601901802 28825486102

Updated on May 13, 2020

Comments

  • Martijn Courteaux
    Martijn Courteaux almost 4 years

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

  • Admin
    Admin almost 14 years
    Some memory can't be reclaimed by the OS - such as what?
  • Dai Doan
    Dai Doan almost 14 years
    On some real-time OS's, I've seen special Ethernet buffer blocks that don't get cleaned up when a process exits.
  • Dai Doan
    Dai Doan almost 14 years
    Shared memory is also not usually reclaimed, because you may want it to persist for transient processes to use.

Related