Any way to reserve but not commit memory in linux?

前端 未结 4 1198
南方客
南方客 2020-12-01 03:00

Windows has VirtualAlloc, which allows you to reserve a contiguous region of address space, but not actually use any physical memory. Later when you want to use it (or part

相关标签:
4条回答
  • 2020-12-01 03:22

    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).

    0 讨论(0)
  • 2020-12-01 03:27

    "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.

    0 讨论(0)
  • 2020-12-01 03:32

    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

    0 讨论(0)
  • 2020-12-01 03:41

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

    0 讨论(0)
提交回复
热议问题