如何在Linux中保留虚拟内存? [英] How can I reserve virtual memory in Linux?

查看:278
本文介绍了如何在Linux中保留虚拟内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MEM_RESERVE标志在Windows上使用VirtualAllocEx保留连续内存块的应用程序.这将保留一个虚拟内存块,但不会使用物理页面或页面文件块对其进行备份.因此,访问分配的内存将导致分段错误-但其他分配不会与此虚拟内存块相交.

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.

如何使用mmap在Linux上执行相同的操作?我确实在此问题中注意到了答案,但是这是否真的保证说,如果我不触摸分配的页面,将不会为进程分配1 GB的物理内存?我不想遇到任何麻烦的问题.

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.

推荐答案

我相信您应该可以通过使用PROT_NONE映射匿名内存来实现相同的目的.访问PROT_NONE内存将导致段错误,但是该内存区域将被保留并且不能用于任何其他目的.如果要分配很大的内存,请添加MAP_NORESERVE以确保默认的过量使用机制不会检查您的分配.

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通常用于堆栈末尾的保护"页面.

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

这篇关于如何在Linux中保留虚拟内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆