DavidSGK
DavidSGK

Reputation: 3

Java: How to Create Large 2-Dimensional Array

I'm trying to write a simple program that will simulate a scenario described in a probability question. In order to do so, I tried to create and populate a large 2-dimensional array, but the compiler continues to say that the java heap size isn't large enough and when I allocate more memory to the JVM, the process eats up my CPU to the point where my laptop crashes (mind you, it's an ultrabook with an i7 and 8GB of ram). Is this just not possible/recommended in Java? Is there another way? Please help!

This is the line that's the problem (2-dimensional array with 5^12 rows and 12 columns):

int[][] sequences = new int[244140625][12];

P.S. I'm a bit of a beginner when it comes to programming... thank you in advance!

Upvotes: 0

Views: 640

Answers (4)

Hamdy Abd El Fattah
Hamdy Abd El Fattah

Reputation: 1827

Tru this

ArrayList<Integer>[] arr= (ArrayList<Integer>[])new ArrayList[n];

Then

for (int i = 0; i < n; i++) {
    arr[i] = new ArrayList<Integer>();
}

Then

    arr[0].add(5);

Upvotes: 0

Andreas
Andreas

Reputation: 159114

It's actually worse than they say.

An int[12] is (at minimum) 4 bytes for class pointer + 4 bytes for array length + 12 * 4 bytes for values = 56 bytes.

The outer array then uses 4 bytes per pointer, so you get 244,140,625 * (56 + 4) = 14,648,437,500 = 13.6 Gb of memory.

It might work if you run 64-bit Java with -Xmx16g, but it'll run forever with all the paging going on when you only have 8 Gb, and that assumes that pointers are only 4 bytes (compressed).

Upvotes: 2

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

Your array size

 244140625*12*4 = 11718750000 byte
 11718750000%(1024*1024*2024) = 10.91 GB

So, only your array need 10.91 GB memory, so it is not feasible to implement it with array. Alternatively you can use database like MySQL, PostgreSQL or other DBMS solutions those are built for handle huge amount of data.

Upvotes: 0

zapl
zapl

Reputation: 63955

each int in Java is 4 byte large. 244140625 of them take already 931MiB of RAM. You want this x 12. So 10.91GiB. Java could to my knowledge handle that but those 8 gig of RAM you have are not enough.

A database like H2 (http://www.h2database.com/html/main.html) is easy to integrate and can swap to disk. Or you come up with a better algorithm that doesn't need to store 5^12x12 values.

Upvotes: 4

Related Questions