The answer is most often.
Freeing up the heap is the responsiveness of the OS. Although most OS (especially the main OS) frees up a bunch on exit, this does not necessarily apply to the embedded system OS.
When you call memory allocation in a heap, a system call is made for the kernel space of the OS to provide that memory. This memory maps to your process structure, which is supported by the OS. When your program exits, the OS goes through a clean routing, closing all file descriptors and placing this memory for free for allocation to other processes (by the way).
Some of these answers are incorrect, saying that they depend on the compiler. The compiler does not say "free all this memory at the output of the program." It makes no sense, what happens if the OS unexpectedly terminates the program? No, the compiler is responsible for generating system calls whenever memory allocation / deallocation is explicitly requested for the heap.
source share