且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以在Linux中分配大量的虚拟内存吗?

更新时间:2023-11-14 20:39:58

是否可以在linux中分配大量虚拟内存?

Is it possible to allocate large amount of virtual memory in linux?

可能.但是您可能需要对其进行配置以使其被允许:

Possibly. But you may need to configure it to be allowed:

Linux内核支持以下过量使用处理模式

The Linux kernel supports the following overcommit handling modes

0-启发式过量使用处理.地址明显过量使用空间被拒绝.用于典型的系统.它确保认真狂放分配失败,同时允许过量使用以减少交换用法.允许root在此分配更多的内存模式.这是默认设置.

0 - Heuristic overcommit handling. Obvious overcommits of address space are refused. Used for a typical system. It ensures a seriously wild allocation fails while allowing overcommit to reduce swap usage. root is allowed to allocate slightly more memory in this mode. This is the default.

1-始终过量使用.适用于某些科学应用.经典示例是使用稀疏数组的代码,仅依赖于虚拟内存几乎完全由零页组成.

1 - Always overcommit. Appropriate for some scientific applications. Classic example is code using sparse arrays and just relying on the virtual memory consisting almost entirely of zero pages.

2-不要过度使用.系统的总地址空间提交不允许超过交换+可配置的数量(默认值为50%) 的物理 RAM.在大多数情况下,取决于您使用的数量情况,这意味着在访问过程中进程不会被杀死页,但会在适当的内存分配上收到错误.

2 - Don't overcommit. The total address space commit for the system is not permitted to exceed swap + a configurable amount (default is 50%) of physical RAM. Depending on the amount you use, in most situations this means a process will not be killed while accessing pages but will receive errors on memory allocation as appropriate.

对于希望保证其内存的应用程序很有用分配将在将来可用,而无需初始化每个页面.

Useful for applications that want to guarantee their memory allocations will be available in the future without having to initialize every page.

通过sysctl`vm.overcommit_memory'设置过量使用策略.

The overcommit policy is set via the sysctl `vm.overcommit_memory'.

因此,如果您要分配的虚拟内存多于物理内存,那么您需要:

So, if you want to allocate more virtual memory than you have physical memory, then you'd want:

# in shell
sysctl -w vm.overcommit_memory=1

RLIMIT_AS进程的虚拟内存的最大大小(地址空间),以字节为单位.此限制影响对brk(2),mmap(2)和mremap(2)的调用,这些调用在超出此限制时会失败,并显示错误ENOMEM.自动堆栈扩展也会失败(如果没有通过sigaltstack(2)提供备用堆栈,则会生成SIGSEGV,该进程将终止该进程).由于该值很长,因此在32位长的计算机上,此限制最多为2 GiB,或者此资源是无限的.

RLIMIT_AS The maximum size of the process's virtual memory (address space) in bytes. This limit affects calls to brk(2), mmap(2) and mremap(2), which fail with the error ENOMEM upon exceeding this limit. Also automatic stack expansion will fail (and generate a SIGSEGV that kills the process if no alternate stack has been made available via sigaltstack(2)). Since the value is a long, on machines with a 32-bit long either this limit is at most 2 GiB, or this resource is unlimited.

所以,您想要:

setrlimit(RLIMIT_AS, {
    .rlim_cur = RLIM_INFINITY,
    .rlim_max = RLIM_INFINITY,
});

或者,如果您不能授予进程执行此操作的权限,则可以在/etc/security/limits.conf中永久配置此设置,这将影响(用户/组的)所有进程.

Or, if you cannot give the process permission to do this, then you can configure this persistently in /etc/security/limits.conf which will affect all processes (of a user/group).

好的,因此mmap似乎支持...,但是它需要文件描述符....可能是一个胜利,但是如果必须要有文件支持的话就不是...我不喜欢附加到文件的想法

Ok, so mmap seems to support ... but it requires a file descriptor. ... could be a win but not if they have to be backed by a file ... I don't like the idea of attaching to a file

您不需要使用文件支持的mmap.有MAP_ANONYMOUS.

You don't need to use a file backed mmap. There's MAP_ANONYMOUS for that.

我不知道要输入什么号码

I did not know what number to put in to request

然后使用null.示例:

Then use null. Example:

mmap(nullptr, 256*GB, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)

也就是说,如果您已经按照说明配置了系统,那么 new 应该和 mmap 一样好.可能会使用 malloc ,而对于这样的大型分配,可能会使用 mmap .

That said, if you've configured the system as described, then new should work just as well as mmap. It'll probably use malloc which will probably use mmap for large allocations like this.

奖金提示:利用 查看全文