C/C++ Programming : Do you know that free or delete will NOT release memory back to OS ?
Note: Based on observations and tests made on Solaris OS 10 with Solaris compilers.
When you call free() or delete(), it will NOT really release any memory back to OS. Instead, that memory is kept with the same process until it is terminated. However, this memory can be reused for any future allocations by the same process.
This ‘freed’ memory is never released to OS, even when the system is out of memory and other processes need memory. That means, a process is stuck with the largest level of memory allocation throughout its life time.
This is big concern for most processes that are expected to run (literally) forever without a restart.
There is an alternative, by linking to mapmalloc library, that uses map/unmap instead of sbrk for memory allocation. When linked with this library, memory could be really released to operating system when free/delete is called. However, memory allocation could be potentially 5 times slower. One heck of a trade off!
Hope this information will help in your designs.
When you call free() or delete(), it will NOT really release any memory back to OS. Instead, that memory is kept with the same process until it is terminated. However, this memory can be reused for any future allocations by the same process.
This ‘freed’ memory is never released to OS, even when the system is out of memory and other processes need memory. That means, a process is stuck with the largest level of memory allocation throughout its life time.
This is big concern for most processes that are expected to run (literally) forever without a restart.
There is an alternative, by linking to mapmalloc library, that uses map/unmap instead of sbrk for memory allocation. When linked with this library, memory could be really released to operating system when free/delete is called. However, memory allocation could be potentially 5 times slower. One heck of a trade off!
Hope this information will help in your designs.
Comments