有什么办法可以保留但不能在Linux中提交内存? [英] Any way to reserve but not commit memory in linux?

查看:118
本文介绍了有什么办法可以保留但不能在Linux中提交内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows具有VirtualAlloc,它允许您保留地址空间的连续区域,但实际上不使用任何物理内存.稍后,当您要使用它(或其中的一部分)时,可以再次调用VirtualAlloc来提交以前保留的页面的区域.

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 of it) you call VirtualAlloc again to commit the region of previously reserved pages.

这实际上非常有用,但是我最终希望将我的应用程序移植到linux-因此,如果以后无法移植,我就不想使用它. linux有办法做到这一点吗?

This is actually really useful, but I want to eventually port my application to linux - so I don't want to use it if I can't port it later. Does linux have a way to do this?

编辑-用例

我正在考虑分配4 GB或类似的虚拟地址空间,但一次只能分配64K.这将为我提供一种零拷贝方式,可将阵列增长到最大4 GB.这很重要,因为通常将数组大小和副本加倍会导致非常大的数组出现看似随机的,不可接受的延迟.

I'm thinking of allocating 4 GB or some such of virtual address space, but only committing it 64K at a time. This would give me a zero-copy way to grow an array up to 4 GB. Which is important, because the typical double the array size and copy introduces seemingly random unacceptable latency for very large arrays.

推荐答案

mmap a特殊文件,例如/dev/zero(或使用MAP_ANONYMOUS)作为PROT_NONE,以后使用 mprotect 提交.

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

这篇关于有什么办法可以保留但不能在Linux中提交内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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