【问题标题】:Linux: Cannot allocate more than 32 GB/64 GB of memory in a single process due to virtual memory limitLinux:由于虚拟内存限制,无法在单个进程中分配超过 32 GB/64 GB 的内存
【发布时间】:2015-05-11 16:40:30
【问题描述】:

我有一台运行 Linux (3.19.5-200.fc21.x86_64) 的 128 GB RAM 的计算机。但是,我不能在单个进程中分配超过 ~30 GB 的 RAM。除此之外,malloc 失败:

#include <stdlib.h>
#include <iostream>

int main()
{
   size_t gb_in_bytes = size_t(1)<<size_t(30); // 1 GB in bytes (2^30).
   // try to allocate 1 block of 'i' GB.
   for (size_t i = 25; i < 35; ++ i) {
      size_t n = i * gb_in_bytes;
      void *p = ::malloc(n);
      std::cout << "allocation of 1 x " << (n/double(gb_in_bytes)) << " GB of data. Ok? " << ((p==0)? "nope" : "yes") << std::endl;
      ::free(p);
   }
}

这会产生以下输出:

/tmp> c++ mem_alloc.cpp && a.out

allocation of 1 x 25 GB of data. Ok? yes
allocation of 1 x 26 GB of data. Ok? yes
allocation of 1 x 27 GB of data. Ok? yes
allocation of 1 x 28 GB of data. Ok? yes
allocation of 1 x 29 GB of data. Ok? yes
allocation of 1 x 30 GB of data. Ok? yes
allocation of 1 x 31 GB of data. Ok? nope
allocation of 1 x 32 GB of data. Ok? nope
allocation of 1 x 33 GB of data. Ok? nope
allocation of 1 x 34 GB of data. Ok? nope

找了半天,发现这和最大虚拟内存大小有关:

~> ulimit -all
[...]
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
virtual memory          (kbytes, -v) 32505856
[...]

我可以通过ulimit -v 64000000 将此限制增加到 ~64 GB,但不能进一步。除此之外,我收到operation not permitted 错误:

~> ulimit -v 64000000
~> ulimit -v 65000000                                                                                                                                  
bash: ulimit: virtual memory: cannot modify limit: Operation not permitted                                                                              
~> ulimit -v unlimited
bash: ulimit: virtual memory: cannot modify limit: Operation not permitted 

更多搜索表明,原则上应该可以通过/etc/security/limits.conf 中的“as”(地址空间)条目设置这些限制。但是,通过这样做,我只能减少最大虚拟内存量,而不能增加它。

有没有办法完全解除每个进程的虚拟内存限制,或者将其增加到 64 GB 以上?我想在单个应用程序中使用所有物理内存。

编辑:

  • 跟随 Ingo Leonhardt,我在以 root 而非标准用户身份登录后尝试了 ulimits -v unlimited。这样做可以解决 root 的问题(程序可以在以 root 身份登录时分配所有物理内存)。但这仅适用于root,不适用于其他用户。但是,至少这意味着原则上内核可以很好地处理这个问题,并且只是配置问题。

  • 关于limits.conf:我尝试明确添加

    • 硬如无限制
    • 软如无限

    /etc/security/limits.conf,然后重新启动。这没有效果。以标准用户身份登录后,ulimit -v 仍然返回大约 32 GB,ulimit -v 65000000 仍然显示permission denied(而ulimit -v 64000000 有效)。 limits.conf 的其余部分被注释掉,而在 /etc/security/limits.d 中只有一个其他不相关的条目(对于非 root 用户,将 nproc 限制为 4096)。也就是说,虚拟内存限制必须来自limits.conf 以外的其他地方。任何想法还有什么可能导致ulimits -v 不是“无限”的?

编辑/解决方案:

  • 这是我自己的愚蠢造成的。我的用户设置中有一个(早已被遗忘的)程序,它使用setrlimit 来限制每个进程的内存量,以防止 Linux 死机。它无意中从 32 GB 机器复制到 128 GB 机器。感谢 Paul 和 Andrew Janke 以及其他所有人帮助追踪它。对不起大家:/。

  • 如果其他人遇到这种情况:在 bash 和配置文件设置中搜索 ulimit/setrlimit,以及可能调用这些设置的程序(包括您自己的和系统范围的 /etc 设置)并确保 /security/limits.conf 确实如此不包括这个限制...(或者至少尝试创建一个新用户,看看这是否发生在您的用户或系统设置中)

【问题讨论】:

  • 如果有人想在没有 128GB 内存机器的情况下玩这个,在 Amazon EC2 上,r3.4xlarge 有 122GB。不过零售价为 1.30 美元/小时,尚未查看现货市场。
  • 我认为 C 没有 std::cout...
  • 尝试以 root 身份调用 ulimit。至少 ulimit -v 65000000ulimit -v unlimited 都应该成功
  • @cgk 当你这样做时,你是重新启动还是只注销/登录? ulimit -v 在启动时自动在/etc/rc.local 中运行(不是由您手动运行)是什么?您可以以 root 身份将 ulimit -v &gt;/tmp/ulimit.out 编辑为 /etc/rc.local 以通过查看将创建的 /tmp/ulimit.out 文件来找出这一点。这是尝试查看它是否在初始化时设置并逐渐下降,或者是否在您登录时设置。

标签: linux memory


【解决方案1】:

这是一个 ulimit 和系统设置问题,而不是 c++ 问题。

我可以毫无问题地在 Amazon EC2 实例类型 r3.4xlarge 上运行您经过适当修改的代码。这些在现货市场上的成本不到 0.20 美元/小时,所以我建议你租一个,也许看看 /etc 并与你自己的设置进行比较......或者你可能需要重新编译一个 Linux 内核才能使用它很多内存...但这不是 C++ 或 gcc 问题。

EC2 机器上的 Ubuntu 已经设置为无限的进程内存。

$ sudo su
# ulimit -u
--> unlimited

这个有 125GB 内存

# free
             total       used       free     shared    buffers     cached
Mem:     125903992    1371828  124532164        344      22156     502248
-/+ buffers/cache:     847424  125056568
Swap:            0          0          0

我将您的程序的限制修改为最高 149GB。

这是输出。高达 118GB 看起来不错。

root@ip-10-203-193-204:/home/ubuntu# ./memtest
allocation of 1 x 25 GB of data. Ok? yes
allocation of 1 x 26 GB of data. Ok? yes
allocation of 1 x 27 GB of data. Ok? yes
allocation of 1 x 28 GB of data. Ok? yes
allocation of 1 x 29 GB of data. Ok? yes
allocation of 1 x 30 GB of data. Ok? yes
allocation of 1 x 31 GB of data. Ok? yes
allocation of 1 x 32 GB of data. Ok? yes
allocation of 1 x 33 GB of data. Ok? yes
allocation of 1 x 34 GB of data. Ok? yes
allocation of 1 x 35 GB of data. Ok? yes
allocation of 1 x 36 GB of data. Ok? yes
allocation of 1 x 37 GB of data. Ok? yes
allocation of 1 x 38 GB of data. Ok? yes
allocation of 1 x 39 GB of data. Ok? yes
allocation of 1 x 40 GB of data. Ok? yes
allocation of 1 x 41 GB of data. Ok? yes
allocation of 1 x 42 GB of data. Ok? yes
allocation of 1 x 43 GB of data. Ok? yes
allocation of 1 x 44 GB of data. Ok? yes
allocation of 1 x 45 GB of data. Ok? yes
allocation of 1 x 46 GB of data. Ok? yes
allocation of 1 x 47 GB of data. Ok? yes
allocation of 1 x 48 GB of data. Ok? yes
allocation of 1 x 49 GB of data. Ok? yes
allocation of 1 x 50 GB of data. Ok? yes
allocation of 1 x 51 GB of data. Ok? yes
allocation of 1 x 52 GB of data. Ok? yes
allocation of 1 x 53 GB of data. Ok? yes
allocation of 1 x 54 GB of data. Ok? yes
allocation of 1 x 55 GB of data. Ok? yes
allocation of 1 x 56 GB of data. Ok? yes
allocation of 1 x 57 GB of data. Ok? yes
allocation of 1 x 58 GB of data. Ok? yes
allocation of 1 x 59 GB of data. Ok? yes
allocation of 1 x 60 GB of data. Ok? yes
allocation of 1 x 61 GB of data. Ok? yes
allocation of 1 x 62 GB of data. Ok? yes
allocation of 1 x 63 GB of data. Ok? yes
allocation of 1 x 64 GB of data. Ok? yes
allocation of 1 x 65 GB of data. Ok? yes
allocation of 1 x 66 GB of data. Ok? yes
allocation of 1 x 67 GB of data. Ok? yes
allocation of 1 x 68 GB of data. Ok? yes
allocation of 1 x 69 GB of data. Ok? yes
allocation of 1 x 70 GB of data. Ok? yes
allocation of 1 x 71 GB of data. Ok? yes
allocation of 1 x 72 GB of data. Ok? yes
allocation of 1 x 73 GB of data. Ok? yes
allocation of 1 x 74 GB of data. Ok? yes
allocation of 1 x 75 GB of data. Ok? yes
allocation of 1 x 76 GB of data. Ok? yes
allocation of 1 x 77 GB of data. Ok? yes
allocation of 1 x 78 GB of data. Ok? yes
allocation of 1 x 79 GB of data. Ok? yes
allocation of 1 x 80 GB of data. Ok? yes
allocation of 1 x 81 GB of data. Ok? yes
allocation of 1 x 82 GB of data. Ok? yes
allocation of 1 x 83 GB of data. Ok? yes
allocation of 1 x 84 GB of data. Ok? yes
allocation of 1 x 85 GB of data. Ok? yes
allocation of 1 x 86 GB of data. Ok? yes
allocation of 1 x 87 GB of data. Ok? yes
allocation of 1 x 88 GB of data. Ok? yes
allocation of 1 x 89 GB of data. Ok? yes
allocation of 1 x 90 GB of data. Ok? yes
allocation of 1 x 91 GB of data. Ok? yes
allocation of 1 x 92 GB of data. Ok? yes
allocation of 1 x 93 GB of data. Ok? yes
allocation of 1 x 94 GB of data. Ok? yes
allocation of 1 x 95 GB of data. Ok? yes
allocation of 1 x 96 GB of data. Ok? yes
allocation of 1 x 97 GB of data. Ok? yes
allocation of 1 x 98 GB of data. Ok? yes
allocation of 1 x 99 GB of data. Ok? yes
allocation of 1 x 100 GB of data. Ok? yes
allocation of 1 x 101 GB of data. Ok? yes
allocation of 1 x 102 GB of data. Ok? yes
allocation of 1 x 103 GB of data. Ok? yes
allocation of 1 x 104 GB of data. Ok? yes
allocation of 1 x 105 GB of data. Ok? yes
allocation of 1 x 106 GB of data. Ok? yes
allocation of 1 x 107 GB of data. Ok? yes
allocation of 1 x 108 GB of data. Ok? yes
allocation of 1 x 109 GB of data. Ok? yes
allocation of 1 x 110 GB of data. Ok? yes
allocation of 1 x 111 GB of data. Ok? yes
allocation of 1 x 112 GB of data. Ok? yes
allocation of 1 x 113 GB of data. Ok? yes
allocation of 1 x 114 GB of data. Ok? yes
allocation of 1 x 115 GB of data. Ok? yes
allocation of 1 x 116 GB of data. Ok? yes
allocation of 1 x 117 GB of data. Ok? yes
allocation of 1 x 118 GB of data. Ok? yes
allocation of 1 x 119 GB of data. Ok? nope
allocation of 1 x 120 GB of data. Ok? nope
allocation of 1 x 121 GB of data. Ok? nope
allocation of 1 x 122 GB of data. Ok? nope
allocation of 1 x 123 GB of data. Ok? nope
allocation of 1 x 124 GB of data. Ok? nope
allocation of 1 x 125 GB of data. Ok? nope
allocation of 1 x 126 GB of data. Ok? nope
allocation of 1 x 127 GB of data. Ok? nope
allocation of 1 x 128 GB of data. Ok? nope
allocation of 1 x 129 GB of data. Ok? nope
allocation of 1 x 130 GB of data. Ok? nope
allocation of 1 x 131 GB of data. Ok? nope
allocation of 1 x 132 GB of data. Ok? nope
allocation of 1 x 133 GB of data. Ok? nope
allocation of 1 x 134 GB of data. Ok? nope
allocation of 1 x 135 GB of data. Ok? nope
allocation of 1 x 136 GB of data. Ok? nope
allocation of 1 x 137 GB of data. Ok? nope
allocation of 1 x 138 GB of data. Ok? nope
allocation of 1 x 139 GB of data. Ok? nope
allocation of 1 x 140 GB of data. Ok? nope
allocation of 1 x 141 GB of data. Ok? nope
allocation of 1 x 142 GB of data. Ok? nope
allocation of 1 x 143 GB of data. Ok? nope
allocation of 1 x 144 GB of data. Ok? nope
allocation of 1 x 145 GB of data. Ok? nope
allocation of 1 x 146 GB of data. Ok? nope
allocation of 1 x 147 GB of data. Ok? nope
allocation of 1 x 148 GB of data. Ok? nope
allocation of 1 x 149 GB of data. Ok? nope

现在,我在这上面花了大约 0.17 美元……

【讨论】:

  • 非常有趣。几乎和 OP 计划用 128 GB 内存做的事情一样有趣。
  • @Carlton 另一个有趣的问题是,为什么人们对有保证的虚拟机收费如此之高,而不是可能会消失的现货虚拟机。把一个变成另一个有那么难吗?
  • @Paul 当然。如果你想跳过那些圈子,那就去做吧。它将产生开发成本。构建故障转移系统需要多少小时的开发成本,每个“大型”CPU 集群每小时可以节省 1 美元?取决于您租用多少小时的“大型”CPU 集群。
  • @cgk 我注意到我全新安装的 64 位 Ubuntu 15.04 具有无限的 ulimit。也许您需要拉出主硬盘驱动器,将它们放在一边,弹出另一个驱动器,然后从 DVD 或 USB 记忆棒重新安装。我使用了ulimit 命令,发现它是由bash 定义的ulimit 不是文件系统中可执行的命令,它在 bash 中,并且它还有一个手册页作为 C 中的 deprecated 系统调用。来自 shell,help ulimit 即使是 root,我无法使用ulimit -H -v [number of kB] 设置硬限制。我可以以 root 身份设置和撤消软限制。建议您尝试重新安装。
  • 我相信替换 ulimit 的较新的 POSIX 系统调用是 setrlimit/getrlimitpubs.opengroup.org/onlinepubs/9699919799/functions/…。我不知道他们为 Fedora 配置的默认设置在哪里,但也许这是一个线索。
猜你喜欢
  • 2012-04-04
  • 2019-04-11
  • 2023-03-30
  • 2021-03-01
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2019-08-16
  • 2017-04-08
相关资源
最近更新 更多