How can I reserve virtual memory in Linux?

ε祈祈猫儿з 提交于 2019-12-01 05:43:43

问题


I have an application that reserves a contiguous memory block using VirtualAllocEx on Windows with the MEM_RESERVE flag. This reserves a virtual memory block, but does not back it with a physical page or page file chunk. Hence, accessing the allocated memory will result in a segmentation fault -- but other allocations will not intersect with this virtual memory block.

How can I do the same for Linux with mmap? I did notice the answer in this question, but does that really guarantee that say, 1 GB of physical memory won't be allocated to my process if I don't touch the allocated pages? I don't want any thrashing problems.


回答1:


I believe you should be able to achieve the same by mapping anonymous memory with PROT_NONE. Accessing PROT_NONE memory will result in a segfault, but the memory region will be reserved and not used for any other purpose. If you want to allocate a very big chunk of memory, add MAP_NORESERVE to ensure that the default overcommit mechanism won't check your allocation.

PROT_NONE is commonly employed for "guard" pages at the end of stacks.



来源:https://stackoverflow.com/questions/15261527/how-can-i-reserve-virtual-memory-in-linux

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