The OS free the space for you when it removes the process's descriptor (task_struct
in Linux's case) from its process list.
The compiler usually generates an exit()
system call and there's where all this is handled.
On Linux, basically, all these happens in the kernel's exit_mm()
function, eventually invoked by exit()
. It will release the address space owned by the process with mm_release()
function. All that source is available for you to read (although it might get a little complicated :) But yes, the operating system is in charge of releasing process's resources.
And just because I like it so much, if you're into these topics, this is a very GOOD reading: Understanding the Linux Kernel.