How to return memory from process to the OS

前端 未结 5 624
走了就别回头了
走了就别回头了 2020-12-16 02:54

I have an issue with memory management in various operating systems.

My program is a server that does some processing that could take a few GB of memory. After that,

5条回答
  •  无人及你
    2020-12-16 03:29

    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().

提交回复
热议问题