1

I want to make an array of size 10^9 elements where each elements can be an integer of the same size. I always get an OutOfMemoryException at the initialization line. How can I achieve this?

If this is not possible, please suggest alternative strategies?

11
  • Please share your code to us.. Commented Jan 28, 2013 at 11:25
  • Declaring array is simple. int []array = new int [size]; You getting runtime exception? Commented Jan 28, 2013 at 11:26
  • 6
    1 billion elements of int?! => 4Gb on the heap. Commented Jan 28, 2013 at 11:27
  • "where each elements can be an integer of the same size".. :S (it's an array.. not an ArrayList or Tuple). And get more RAM I guess. Commented Jan 28, 2013 at 11:27
  • I mean that My array is not allowed to have elements repeated, so if its size is 10^9 elements, then it needs to store elements of range 10^9 each. Commented Jan 28, 2013 at 11:28

3 Answers 3

6

Arrays are limited to 2GB in .net 4.0 or earlier, even in a 64 bit process. So with one billion elements, the maximum supported element size is two bytes, but an int is four bytes. So this will not work.

If you want to have a larger collection, you need to write it yourself, backed by multiple arrays.

In .net 4.5 it's possible to avoid this limitation, see Jon Skeet's answer for details.

Sign up to request clarification or add additional context in comments.

5 Comments

are you sure that arrays are limited to 2GB in 64 bit? do you have any reference?
"By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit. For more information, see the <gcAllowVeryLargeObjects> element."
@SonerGönül this is for 32 bit as you have stated; I am not confident whether there is a limitation for 64 bit.
@CodesInChaos then I think you need to change the answer.
5

Assuming you mean int as the element type, you can do this using .NET 4.5, if you're using a 64-bit CLR.

You need to use the <gcAllowVeryLargeObjects> configuration setting. This is not on by default.

If you're using an older CLR or you're on a 32-bit machine, you're out of luck. Of course, if you're on a 64-bit machine but just an old version of the CLR, you could encapsulate your "one large array" into a separate object which has a list of smaller arrays... you could even implement IList<int> that way so that most code wouldn't need to know that you weren't really using a single array.

(As noted in comments, you'll still only be able to create an array with 231 elements; but your requirement of 109 is well within this.)

4 Comments

Interesting, didn't know .net 4.5 allows this.
In a 32 bit process he's out of luck, even when using multiple arrays. 4GB simply doesn't fit the address space. So he'd need to keep part of the data elsewhere.
Note that even if you enable this, you will STILL be limited to a max of approx 2^31 addressable elements regardless of their size.
@MatthewWatson: True, but that's well within the 10^9 required by the OP. Will edit it in anyway.
0

I think you shouldn't load all this data into memory, store it somewhere in a file, and make a class that could work as an array but actually reads and writes data from and in the file

this is the general idea (this won't work as it is of course, plus you'll have to convert your int values into byte[] array before writing it and some other things)

public class FileArray
{
   Stream s;

   public this[int index]
   {
      get { s.Position = index * 4; return s.Read(); }
      set { s.Position = index * 4; s.Write(value); }
   }
}

that way you would have something that works just like an array, but the data would be stored on your hard drive

1 Comment

Done! That's the way I'd deal with this problem (btw this is pseudo code just to explain a little better the idea)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.