6

I am getting a 'ArgumentError: array size too big' message with the following code:

MAX_NUMBER = 600_000_000
my_array = Array.new(MAX_NUMBER)

Question. What is the max value that the Array.new function takes in Ruby?

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • `NoMemoryError: failed to allocate memory` here. What ruby are you using? – Reactormonk Sep 10 '10 at 19:01
  • Why do you want to make Ruby cry? :) "Array.new(100_000_000) && false" seems to run somewhat fast. – Jason Noble Sep 10 '10 at 19:21
  • :-) Yup, it worked fine for me when I had MAX_NUMBER set to 500_000_000 - took five minutes to finish though. However, that did not solve the main problem that I was working with. Will have to rethink my solution as well, I guess. – Prakash Murthy Sep 10 '10 at 19:33

1 Answers1

16

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.

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).

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.

Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653