ramoh
ramoh

Reputation: 153

Getting out of memory exception while creating 2 Dimensional int array

I have:

int [][] lengths=null;

I have to initialize this based on runtime parameter.

I am getting OutOfMemoryException when my array size something like int[13000][130000] or more.

Is there a way to come around this or any other approach I can take ?

Upvotes: 0

Views: 157

Answers (3)

Christian St.
Christian St.

Reputation: 1911

An integer takes 4 byte of memory. You want to allocate 4 Byte * 13000 * 130000 = 6760000000 Byte. That are ~6,5 gigabyte of memory.

If your computer has that amount of memory, there exist JVM parameters to increase the maximum used by the JVM.

Upvotes: 0

irrenhaus3
irrenhaus3

Reputation: 309

If it isn't absolutely neccessary for you to fill every single cell of this grid, nested lists might be a smarter approach. Think of it as an 1D-array filled with 1D-arrays of various size. As a data type, they are especially useful for values provided during runtime, so if you don't want to or are not allowed to increase your mem cap as described in Salahs answer, think of this as an alternative.

Upvotes: 0

Salah
Salah

Reputation: 8657

Note: this should not prevent the OutOfMemoryError but you need to learn about the JVM memory arguments, because it should help for any case.

You need to take a look at the JVM memory parameters. actually you can set the as much memory as you want to your JVM :

-Xmx2048m -> this param to set the max memory that the JVM can allocate
-Xms1024m -> the init memory that JVM will allocate on the start up
-XX:MaxPermSize=512M -> this for the max Permanent Generation memory

and you may want to check this parameters also.

-XX:MaxNewSize=  -> this could be 40% from your Xmx value
-XX:NewSize=614m -> this could be 40% from your Xmx value

also you may tell you JVM what type of GC to use like (i think its already use by default in the earlier versions)

-XX:+UseConcMarkSweepGC

Upvotes: 1

Related Questions