On Linux, BSD, or OS X, use malloc
. I think the popular "jemalloc" implementation on FreeBSD uses a dedicated mmap
for every region 1 MiB or larger. The smaller regions are still backed by mmap
, so they still give most of the same behavior, but when you free
the smaller regions you won't automatically unmap them. I think. The glibc "dlmalloc" implementation, which is used on Linux, also uses a dedicated mmap
for allocations at least 1 MiB, but I think it uses sbrk
for smaller regions. Mac OS X's malloc also uses mmap
but I am not sure about the particular parameters.
A pointer that you get from a large malloc
will point to a shared page in RAM filled with zero bytes. As soon as you write to a page in that region, a new page in physical RAM will be allocated and filled with zero bytes. So you see, the default behavior of malloc
is already lazy. It's not that the pages are swapped out to start with, it's that they aren't even there to begin with.
If you are done with the data in a region, you can use madvise
with MADV_FREE
. This tells the kernel that it can free the related pages instead of swapping them out. The pages remain valid, and as soon as you write to them they'll turn back into normal pages. This is kind of like calling free
and then malloc
.
Summary: Just use malloc
. It does what you want.