0

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 ?

Maroun
  • 94,125
  • 30
  • 188
  • 241
ramoh
  • 153
  • 3
  • 15
  • Increase the heap size. Have a look http://stackoverflow.com/questions/6452765/how-to-increase-heap-size-of-jvm – An SO User Apr 24 '14 at 08:15
  • What is the Heap size? Arrays need contiguous memory so JVM might not be in position to allocate 13000*130000*32 byte (memory required to store an int in java) for this array. So, it threw OutofMemoryException. First thing is to check whether this huge array is required and if it is required increase the JVM heap. – Abhijith Nagarajan Apr 24 '14 at 08:16
  • You will need 13000*130000*4 bytes to store your array ( if you did not forget a 0 in your 1st number), that's about 6.3Gb, you will need more to let the rest of your program work. I'm afraid increasing the memory of your JVM will not be enough, what is the purpose of having such a big array? – StephaneM Apr 24 '14 at 08:22

3 Answers3

1

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
Salah
  • 8,567
  • 3
  • 26
  • 43
0

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.

Community
  • 1
  • 1
irrenhaus3
  • 126
  • 1
  • 4
0

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.

Christian St.
  • 1,751
  • 2
  • 22
  • 41