How to return memory from process to the OS How to return memory from process to the OS unix unix

How to return memory from process to the OS


The glibc library (which is normally used as the standard C library in Linux) can allocate memory in two ways - with sbrk() or with mmap(). It will use mmap() for large enough allocations.

Memory allocated with sbrk() cannot easily be given up again (only in special cases, and as far as I know glibc doesn't even try). Memory allocated with mmap() can be returned using munmap().

If you depend on being able to return memory to the OS, you can use mmap() directly instead of malloc(); but this will become inefficient if you allocate lots of small blocks. You may need to implement your own pool allocator on top of mmap().


Most of the times, memory won't be returned to the system until the process terminates. Depending on operating system and runtime library, memory might be given back to the system, but I don't know of any reliable way to make sure this will happen.

If processing requires a few GB of memory, have your server wait for the request, then spawn a new process for processing the data - You can communicate with your server using pipes. When processing is done, return the result and terminate the spawned process.


the way memory is allocated (and maybe given back to the OS) is in the libc, i assume. The Programming language / library stack you are using might be of reason for this.

I assume glibc will return non-fragmented memory at the top of the heap. Your process might allocate 10MB of data it will use all the time. After that, 500MB of data that is used in processing will be allocated.After that, a tiny fragment of data that is kept even after the processing (might be the result of the processing) is allocated. After that another 500MB is allocatedMemory layout is:

|10MB used|500 MB processing|1MB result|500MB processing| = 1011 MB total

When the 1000MB are freed, the memory layout is

|10MB used|500MB freed|1MB result|500 MB freed|glibc might now return the memory at the end...|10MB used|500MB freed|1MB result| = 511 MB "in use"also only 11MB of that is used.

I assume that is what happens, you'll need to do further research (seperate memory pools come to mind) to ensure all memory will be freed