为什么创建大于内存的一维数组会失败,但是创建大于内存的2D数组没有问题?

时间:2018-07-27 16:25:34

标签: c++ arrays virtual-memory

为什么创建大于内存的一维数组失败,但是我可以创建大于内存的2D数组?我认为该操作系统可以为您提供虚拟内存,并且您可以根据需要进行请求。直到您开始读取和写入内存,并且硬件约束成为问题后,它才成为驻留集的一部分。

在内存为512MB的小型VM上,我尝试过:

1, 512 MB array: no issue
1, 768 MB array: no issue
1, 879 MB array: no issue
1, 880 MB array: fails
1, 1024 MB array: fails
1000, 512MB arrays no issue (at this point
    I've allocated 256GB of virtual memory,
    well exceeding the physical limits)

在具有8GB内存的大型VM上,以上所有方法均有效。

在此实验中,我使用了以下代码:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* atoi */
#include <iostream>
#include <unistd.h>

int main(int argc, char *argv[],char **envp) {
    if(argc < 3) {
        printf("main <mb> <times>\n");
        return -1;
    }

    int megabytes = atoi(argv[1]);
    int times = atoi(argv[1]);

    // megabytes    1024 kilobytes      1024 bytes          1 integer
    // --------   * ---------        *  ----------    *     --------
    //              megabyte            kilobyte            4 bytes
    int sizeOfArray = megabytes*1024*1024/sizeof(int);
    long long bytes = megabytes*1024*1024;
    printf("grabbing memory :%dmb, arrayEntrySize:%d, times:%d bytes:%lld\n",
                    megabytes, sizeOfArray, times, bytes);

    int ** array = new int*[times];
    for( int i = 0; i < times; i++) {
        array[i] = new int[sizeOfArray];
    }

    while(true) {
        // 1 second to microseconds
        usleep(1*1000000);
    }

    for( int i = 0; i < times; i++) {
        delete [] array[i];
    }
    delete [] array;
}

在小型512MB VM上进行实验的命令和输出:

free -h
              total        used        free      shared  buff/cache   available
Mem:           488M         66M         17M        5.6M        404M        381M
Swap:          511M         72K        511M

./a.out 512 1
grabbing memory :512mb, arrayEntrySize:134217728, times:512 bytes:536870912
./a.out 768 1
grabbing memory :768mb, arrayEntrySize:201326592, times:768 bytes:805306368

./a.out 1024 1
grabbing memory :1024mb, arrayEntrySize:268435456, times:1024 bytes:1073741824
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

./a.out 512 1000
grabbing memory :512mb, arrayEntrySize:134217728, times:512 bytes:536870912
#htop
  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 2768 root      20   0  256G  4912  2764 S  0.0  1.0  0:00.00 ./a.out 512 1000

大型8GB VM上的实验命令和输出:

free -h
              total        used        free      shared      buff/cache   available
Mem:           7.8G         78M        7.6G        8.8M        159M        7.5G
Swap:          511M          0B        511M

./a.out 512 1
grabbing memory :512mb, arrayEntrySize:134217728, times:512 bytes:536870912
./a.out 768 1
grabbing memory :768mb, arrayEntrySize:201326592, times:768 bytes:805306368
./a.out 1024 1
grabbing memory :1024mb, arrayEntrySize:268435456, times:1024 bytes:1073741824
./a.out 512 1000 
grabbing memory :512mb, arrayEntrySize:134217728, times:512 bytes:536870912
# htop
  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 1292 root      20   0  256G  6920  2720 S  0.0  0.1  0:00.00 ./a.out 512 1000

1 个答案:

答案 0 :(得分:1)

这是由于事实,根据您的要求,将大块内存分配成块。

您要的是2D数组中相对较小的块的序列,每个块不一定彼此相邻。

然而,一维阵列非常庞大,需要一个完整尺寸的连续光内存块,即使您有很多可用的内存,也可能没有该大小的块可用。