简体   繁体   English

为std :: vector分配几GB的内存

[英]Allocate several GBs of memory for std::vector

I need to acquire several GB of data from a sensor. 我需要从传感器获取数GB的数据。 When I tried to allocate a big array with malloc (10 or more GB. My system has 32GB) it returns NULL. 当我尝试使用malloc(10或更多GB。我的系统有32GB)分配大数组时,它返回NULL。 So I thought the problem could be solved with a linked list of iterators to vectors. 因此,我认为可以通过将迭代器链接到向量的列表来解决该问题。

However I don't know how to set this up. 但是我不知道该如何设置。 I tried declaring " list< vector::iterator >" but I can't allocate the memory for each vector (e/o should have 1000~2000 elements). 我尝试声明“ list <vector :: iterator>”,但无法为每个向量分配内存(e / o应该具有1000〜2000个元素)。 Do you know any way to do this or maybe a better solution for this big memory allocation? 您知道执行此操作的任何方法,还是针对此大内存分配的更好解决方案?

If you are using a 64-bit operating system, then malloc should be able to allocate the large size with no problem. 如果使用的是64位操作系统,则malloc应该能够毫无问题地分配较大的大小。

For example, this code runs on my windows machine (64-bit windows) and allocates 10GB of ram flawlessly: 例如,此代码在我的Windows计算机(64位Windows)上运行,并完美地分配了10GB的ram:

#include <stdio.h>
#include <malloc.h>
int main(int argc, char *argv[]) {
    long int size = 10L * 1024 * 1024 * 1024;
    printf("size = %ld\n", size);
    char *x = (char *)malloc(size);
    printf("x = 0x%lx\n", x);
    long int i;
    for (i = 0; i < size; i += 1024*1024) {
        x[i] = 'h';
    }
    printf("Done1\n");
}

However, if you have a 32-bit operating system, you'll be in trouble, and can't allocate over some limit (maybe 3 GB, but probably system dependent) 但是,如果您使用的是32位操作系统,则会遇到麻烦,并且无法分配一定的限制(可能是3 GB,但可能取决于系统)

In that case, you'll need to write your data to a file instead. 在这种情况下,您需要将数据写入文件。

However, if you're using a fat filesystem, then you can't write to a file that big either. 但是,如果您使用的是胖文件系统,则也无法写入那么大的文件。 In that case, you'd have to split the data among many files under 2gb in size. 在这种情况下,您必须将数据分割成大小小于2gb的许多文件。

You'd want to actually check the malloc result for NULL to make sure the malloc works and memory could be grabbed. 您需要实际检查malloc结果是否为NULL,以确保malloc可以工作并且可以获取内存。

I usually move to memory mapped files or shared memory maps for this kind of data volumes. 对于这种数据量,我通常移至内存映射文件或共享内存映射。

This way, you're not bound to the amount of physical (process) memory available at all. 这样,您完全不必受限于可用的物理(进程)内存量。 You can let the OS page in and out as required. 您可以根据需要让OS页面进入和退出。 Fragmentation becomes much less of an issue (unless you actually fragment the logical address space, which is quite hard to achieve on 64 bit architectures). 分段变得不再是一个问题(除非您实际对逻辑地址空间进行分段,这在64位体系结构上很难实现)。


More information 更多信息

I have quite a number of answers on SO that show examples of storing vectors and all manner of more complicated data structures in shared memory/mapped files. 我在SO上有很多答案,这些例子显示了在共享内存/映射文件中存储向量和所有更复杂的数据结构的所有方式的示例。 You might want to look for mapped_file_device (from Boost Iostreams) or managed_shared_memory and managed_mapped_file (from Boost Interprocess) 你可能想寻找mapped_file_device (从升压IOSTREAMS)或managed_shared_memorymanaged_mapped_file (从升压进程间)

You will need to allocation this space under Windows 64 bit OS. 您将需要在Windows 64位OS下分配此空间。 You will ALSO have to set "large address space aware" flag, otherwise you can only get 2 GB of RAM due to how the virtual memory system works on Windows. 您还必须设置“大地址空间感知”标志,否则,由于虚拟内存系统在Windows上的工作方式,您只能获得2 GB的RAM。

You may want to look into using a memory mapped file, as suggested by sehe in his answer if you do not absolutely have to have one large 10 GB chunk of continuous memory. 如果您不一定非要拥有一个很大的10 GB连续内存块,那么您可能想研究使用内存映射文件,这是sehe在他的回答中建议的。 If you have to build your application for Windows 32 bit, then this will be the only answer, as Windows 32 bit normally only allows for 2 GB of memory, unless the option is set for "large address space aware" flag, at which point it will allow 3 GB of memory usage. 如果必须为Windows 32位构建应用程序,那么这将是唯一的答案,因为Windows 32位通常仅允许2 GB内存,除非为“大地址空间感知”标志设置了该选项,否则它将允许3 GB的内存使用量。

当您必须处理大块内存时,最好完全跳过malloc并直接转到用于内存分配的操作系统调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM