当前位置: 首页 > 知识库问答 >
问题:

Linux:由于虚拟内存限制,在单个进程中分配的内存不能超过32 GB/64 GB

程旭尧
2023-03-14
#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
~> 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 

编辑:

>

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

    关于limits.conf:我尝试显式地添加

      null

    >

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

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

  • 共有1个答案

    姬昀
    2023-03-14

    这是ulimit和系统设置问题,而不是C++问题。

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

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

    $ sudo su
    # ulimit -u
    --> unlimited
    
    # 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美元的美国...

     类似资料:
    • 我们都知道,直接从内存读写数据要比从硬盘读写数据快得多,因此更希望所有数据的读取和写入都在内存中完成,然而内存是有限的,这样就引出了物理内存与虚拟内存的概念。 物理内存就是系统硬件提供的内存大小,是真正的内存。相对于物理内存,在 Linux 下还有一个虚拟内存的概念,虚拟内存是为了满足物理内存的不足而提出的策略,它是利用磁盘空间虚拟出的一块逻辑内存。用作虚拟内存的磁盘空间被称为 交换空间(又称  

    • 我有一个linux用户,软虚拟内存限制(ulimit-v)设置为aroud 5GB。 考虑到这一点,我试着做: 我的问题是:我关于ulimit-v>=VmSizes之和的假设正确吗?如果不是,软限制实际上是什么意思?是否有可能超过特定用户的软限制,并仍然可以接受它? 顺便说一句,ulimit-v-h被设置为unlimited,这有什么不同。

    • 问题内容: gdb如何在Linux上访问另一个进程虚拟内存?全部通过/ proc完成吗? 问题答案: gdb如何在Linux上访问另一个进程虚拟内存?全部通过/ proc完成吗? 在Linux上读取内存 : 1)如字节数来读取比更少或文件系统不可用或从读取不成功则使用具有读取数据。 这些是函数中的这些条件: 2)如果要读取的字节数大于或等于3 * sizeof(long)并且可用,则使用或(和使用

    • 主要内容:虚拟内存如何工作?,按需分页,虚拟内存管理系统的快照虚拟内存是一种存储方案,为用户提供了一个拥有非常大的主内存的幻觉。 这是通过将辅助存储器的一部分作为主存储器来完成的。 在这种方案中,用户可以加载比可用主存更大的进程,因为存在内存可用于加载进程的错觉。 操作系统不是在主内存中加载一个大进程,而是在主内存中加载多个进程的不同部分。 通过这样做,多程序的程度将会增加,因此CPU利用率也会增加。 虚拟内存如何工作? 在现代语言中,虚拟内存近来变得非常普

    • 当我在Windows(版本7及以上)上运行一个64位程序时,程序试图分配太多的内存(接近100%的物理内存),系统就会停止运行。如果我不及时捕捉它并杀死冒犯的进程,系统将变得无响应,并需要硬重启。显然,程序不应该试图分配这么多内存,但是bug可能会发生,我希望在测试/调试时保护其他正在运行的进程免受代码中任何此类bug的影响。(在32位中,这不是一个问题,因为逻辑内存限制提供了一个系统范围内存阻塞

    • 使用ResNet50预训练的权重我试图构建一个分类器。代码库完全在Keras高级Tensorflow API中实现。完整的代码发布在下面的GitHub链接中。 源代码:使用RestNet50架构进行分类 预训练模型的文件大小为94.7mb。 我加载了预先训练过的文件 并符合模型 在训练数据集中,我有两个文件夹狗和猫,每个文件夹持有近10,000张图像。当我编译脚本时,我得到以下错误 纪元1/1 2