I have the following problem:
I allocate a large chunk of memory (multiple GiB) via mmap using MAP_ANONYMOUS . This piece contains a large hash map that needs to be reset from time to time. Not all matching can be used in every round (not every page fails), so memset not a good idea - it takes too much time.
What is the best strategy to do it fast?
Will be
madvise(ptr, length, MADV_DONTNEED);
ensure that any subsequent calls provide new blank pages?
On the Linux man madvise :
This call does not affect the semantics of the application (except in the case of MADV_DONTNEED ), but may affect its performance. The kernel may ignore the advice.
...
MADV_DONTNEED
Subsequent access to pages in this range will be successful, but will result in reloading the contents of the memory from the base mapped file (see mmap (2)) or zero-fill pages on demand for mappings without a base file.
...
The current version of Linux (2.4.0) sees this system more as a command than as advice ...
Or do I need to munmap and reassign the region again?
It should work on Linux and ideally have the same behavior on OS X.
c virtual-memory mmap
Sergey L.
source share