4

As we know the heap is used for dynamic allocation of memory for an application. How is the heap memory cleared(and hence avoiding memory leaks) in case of abnormal application termination?

Consider the following scenarios:

  • Say an application crashes all of a sudden on Windows or Linux.
  • We force kill an application in linux: kill -9 <process_name>
  • A C++ program in Visual Studio throws an error in the middle of execution.

Is heap management and cleanup any different in the above cases? [Please add more use-case scenarios which might be of interest here]

This question came up in my mind since we always talk about ensuring no memory leak happens in our code. Now how do we handle scenarios where we force close an application which might result into a program exit without calling the memory free-up calls.

And if such memory leak happens repeatedly, is it possible that the OS becomes short of heap memory? Or does the OS have a way of handling it...

Vishal
  • 3,178
  • 2
  • 34
  • 47

4 Answers4

7

Assuming the OS is a typical implementation of Unix or Windows, the heap memory is released by the OS when the application is killed, no matter what method it is killed by.

Obviuously, other OS's may not do exactly this, and it's up to each OS to solve this problem in a meaningful way - I'm not aware of any OS that doesn't "clean up after killed processes", but I'm sure such a thing may exist in some corner of this world.

Edit: There may be OTHER resoources that aren't quite so easily released, such as shared memory or semaphores used by multiple. But most OS's tend to deal with those by releasing the reference of the application being killed, and letting other processes that wait for any "waitable object" (mutex, semaphore, etc) will be "let run".

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 3
    As a side note, destructors will not be called, thus potentially leading to awful situations if you have some critical code (such as buffers flushed to files) in your destructors. – Fabien May 07 '13 at 12:14
  • Yeah, and the second half of your code that wrote the second half of the file may also not have been run yet... - e.g. "zip" that writes most of the compressed file, but not the "directory" that sits at the end of the file... If the code crashes or gets killed [without "catching" the failure], then the results are "incomplete" and "undefined". – Mats Petersson May 07 '13 at 12:18
  • @Mats: Thanks for your input. Your response was quite helpful. – Vishal May 08 '13 at 01:39
2

"The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits" so killing/closing an application abnormally/normally won't leak any memory.

JackSparrow
  • 707
  • 2
  • 10
  • 24
1

As for dynamic memory management you should use RAII(smart pointers is one example) to take care of memory leaks and management during exceptions and so on.
In cases where your application exits, the OS simply reclaims back all the memory it gave to the process. The OS doesn't understand leaks, it simply takes back what it gave to a process. So there is no leak per se. All memory is reclaimed. You might leak other resources(file descriptors etc) but a clever use of RAII should guard you against that.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks for your input. I had been missing out on one obvious point that every process is provided with a separate heap space. – Vishal May 08 '13 at 01:38
  • Actually, I have worked on a real-time OS that didn't have separate heapspace for each process (because sending messages over the heap gets messy if you have to allocate & free every message when it crosses a process boundary, so it's better to have a common heap [for a group of processes or all processes]). It kept track of the allocated memory on a per process basis, and when the process exited/died, it would release all the memory belonging to that process. All that is required is that the OS tracks all memory allocations, not that the heap belongs to the process. – Mats Petersson May 08 '13 at 08:56
0

It doesn't matter how your process closes, any remaining memory allocated from that process is freed by the OS memory manager when it closes. It's good practice for you to free all the memory you allocate before your process dies, but the available heap for the OS/other applications is the same after your process closes either way.

David
  • 27,652
  • 18
  • 89
  • 138