且构网

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

数组大小太大 - 红宝石

更新时间:2023-09-29 22:37:16

拥有500万的元素的数组的大小,2 GiBytes其中 &ndash的;这取决于你使用&NBSP特定的操作系统上;&ndash的;通常是一个进程可以解决的最大值。换句话说:你的数组是比你的地址空间更大

An array with 500 million elements is 2 GiBytes in size, which – depending on the specific OS you are using – is typically the maximum that a process can address. In other words: your array is bigger than your address space.

因此​​,解决方案是显而易见的:要么使数组小(,比方说,打破它在大块),或使地址空间更大(在Linux中,你可以内核打补丁拿到3,3.5甚至4 GiByte地址空间,当然切换到64位操作系统中的的一个64位的Ruby实现(!)也将工作)。

So, the solutions are obvious: either make the array smaller (by, say, breaking it up in chunks) or make the address space bigger (in Linux, you can patch the kernel to get 3, 3.5 and even 4 GiByte of address space, and of course switching to a 64 bit OS and a 64 bit Ruby implementation(!) would also work).

另外,你需要重新考虑你的方法。而不是一个数组,或者类似的东西也许使用 MMAP 。你需要也许延迟加载仅部分。

Alternatively, you need to rethink your approach. Maybe use mmap instead of an array, or something like that. Maybe lazy-load only the parts you need.