Disclaimer: I do have a diploma in CS but this topic was never explicitly explained to me. I am just stiching pieces of what I know into a whole. Please correct me if I get something wrong.
Suppose you have a C program: you call malloc(1MB)
. This function does not allocate any memory pages from the OS. All it does is allocate a range of addresses from virtual memory address space using a heap. Those addresses are not mapped onto any real memory pages yet. At first write into the address, the CPU catches an exception (or interrupt or something) and assigns a new memory page to that address and then the write follows successfully.
Then I call the free()
function. This function also does not release any memory pages back to the OS. All it does is put the address range back into the heap for future reuse.
My point is that memory pages are never released (until process termination). Is that right? There is no syscall for releasing memory pages, is there? And the OS itself cant guess which pages are not used anymore. All it can do is look which pages are Least Recently Used and move those to swap.
Related (somewhat) question: Is forcing an application to release some the allocated memory possible? Also related: https://stackoverflow.com/questions/1421491/does-calling-free-or-delete-ever-release-memory-back-to-the-system