Zero a large memory mapping with `madvise`

前端 未结 3 1602
南旧
南旧 2021-02-06 05:50

I have the following problem:

I allocate a large chunk of memory (multiple GiB) via mmap with MAP_ANONYMOUS. That chunk holds a large hash map w

3条回答
  •  孤独总比滥情好
    2021-02-06 06:14

    On Linux, you can rely on MADV_DONTNEED on an anonymous mapping zeroing the mapping. This isn't portable, though - madvise() itself isn't standardised. posix_madvise() is standardised, but the POSIX_MADV_DONTNEED does not have the same behaviour as the Linux MADV_DONTNEED flag - posix_madvise() is always advisory, and does not affect the semantics of the application.

提交回复
热议问题