Any way to reserve but not commit memory in linux?

て烟熏妆下的殇ゞ 提交于 2019-11-27 18:32:01

mmap a special file, like /dev/zero (or use MAP_ANONYMOUS) as PROT_NONE, later use mprotect to commit.

You can turn this functionality on system-wide by using kernel overcommit. This is usually default setting on many distributions.

Here is the explanation http://www.mjmwired.net/kernel/Documentation/vm/overcommit-accounting

The Linux equivalent of VirtualAlloc() is mmap(), which provides the same behaviours. However as a commenter points out, reservation of contiguous memory is the behaviour of calls to malloc() as long as the memory is not initialized (such as by calloc(), or user code).

"seemingly random unacceptable latency for very large arrays

You could also consider mlock() or mmap() + MAP_LOCKED to mitigate the impact of paging. Many CPUs support huge (aka large) pages, pages larger than 4kb. These larger pages can mitigate the impact of the TLB on streaming reads/writes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!