Is there a memory leak on program exit?

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

+85
c ++ memory-leaks
Jun 04 2018-10-16T00:
source share
6 answers

Yes, a β€œmemory leak” is simply memory that the process no longer refers to and therefore can no longer be free. The OS still keeps track of all the memory allocated for the process, and will free it when this process ends.

In the vast majority of cases, the OS frees up memory - as is the case with the usual "variations" of Windows, Linux, Solaris, etc. However, it is important to note that in specialized environments, such as various real-time operating systems, memory cannot be freed when the program terminates.

+116
Jun 04 2018-10-06T00-06-04
source share

The OS that runs your program usually does a memory cleanup that is not freed explicitly, and processes that do not close explicitly, but this is not guaranteed by the C ++ standard. You may find some kind of built-in device that does not fix memory leaks.

In doing so, Windows and all the Linux distributions I have ever seen eliminate memory leaks.

You can easily create a huge loop of memory leaks to test it yourself. Watch how the amount of RAM used is growing, and then close the program. You will see that the use of RAM is reduced.




Another consideration when using C ++ is that if you do not delete the allocated memory for the heap, then your destructors are also not called. Sometimes you will have other side effects if your destructors are not called.

+29
Jun 04 2018-10-06T00-06-04
source share

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

+12
Jun 04 2018-10-06T00-06-04
source share

Usually yes. Some systems support things like shared memory blocks that are not automatically freed up when you exit the program. Most of them still save the reference count and delete it when all the programs that opened it exit, but some of them do not (for example, 16-bit Windows had several types of elements that would remain distributed, even if nothing not mentioned in them, although it usually crashed for other reasons before it accumulated enough to cause a problem ...)

+8
Jun 04 '10 at 16:10
source share

Depends on what memory you leaked. Some memory cannot be recovered by the OS. Most of the memory on most operating systems, however, will be automatically restored when the process ends.

+2
Jun 04 '10 at 16:08
source share

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

+2
Jun 04 '10 at 16:09
source share



All Articles