Can malloc_trim() release memory from the middle of the heap?

后端 未结 2 1718
傲寒
傲寒 2021-01-01 05:54

I am confused about the behaviour of malloc_trim as implemented in the glibc.

man malloc_trim
[...]
malloc_trim - release free memory from the top of the hea         


        
2条回答
  •  不思量自难忘°
    2021-01-01 06:26

    ... utilizing madvise(x, MADV_DONTNEED) to release memory back to the operating system.

    madvise(x, MADV_DONTNEED) does not release memory. man madvise:

    MADV_DONTNEED
    Do not expect access in the near future. (For the time being, the application is finished with the given range, so the kernel can free resources associated with it.) Subsequent accesses of pages in this range will succeed, but will result either in reloading of the memory contents from the underlying mapped file (see mmap(2)) or zero-fill-on-demand pages for mappings without an underlying file.

    So, the usage of madvise(x, MADV_DONTNEED) does not contradict man malloc_trim's statement:

    This function cannot release free memory located at places other than the top of the heap.

提交回复
热议问题