Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maximum limit on Java array

I am trying to create 2D array in Java as follows:

int[][] adjecancy = new int[96295][96295];

but it is failing with the following error:

JVMDUMP039I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" at 2017/04/07 11:58:55 - please wait.
JVMDUMP032I JVM requested System dump using 'C:\eclipse\workspaces\TryJavaProj\core.20170407.115855.7840.0001.dmp' in response to an event
JVMDUMP010I System dump written to C:\eclipse\workspaces\TryJavaProj\core.20170407.115855.7840.0001.dmp
JVMDUMP032I JVM requested Heap dump using 'C:\eclipse\workspaces\TryJavaProj\heapdump.20170407.115855.7840.0002.phd' in response to an event
JVMDUMP010I Heap dump written to C:\eclipse\workspaces\TryJavaProj\heapdump.20170407.115855.7840.0002.phd

A way to solve this is by increasing the JVM memory but I am trying to submit the code for an online coding challenge. There it is also failing and I will not be able to change the settings there.

Is there any standard limit or guidance for creating large arrays which one should not exceed?

like image 764
Kaushik Lele Avatar asked Apr 07 '17 06:04

Kaushik Lele


People also ask

Is there any limit for array?

An array can have a maximum of eight dimensions. You declared an array whose total size is greater than the maximum allowable size. The maximum allowable array size is 65,536 bytes (64K).

Can size of array be increased in Java?

You can't change the size of the array after it's constructed. However, you can change the number of elements in an ArrayList whenever you want.

How do you declare a large array in Java?

long[] longArray = new long[Integer. MAX_VALUE];


2 Answers

int[][] adjecancy = new int[96295][96295];

When you do that you are trying to allocate 96525*96525*32 bits which is nearly 37091 MB which is nearly 37 gigs. That is highly impossible to get the memory from a PC for Java alone.

I don't think you need that much data in your hand on initialization of your program. Probably you have to look at ArrayList which gives you dynamic allocation of size and then keep on freeing up at runtime is a key to consider.

There is no limit or restriction to create an array. As long as you have memory, you can use it. But keep in mind that you should not hold a block of memory which makes JVM life hectic.

like image 117
Suresh Atta Avatar answered Oct 21 '22 09:10

Suresh Atta


Array must obviously fit into memory. If it does not, the typical solutions are:

  • Do you really need int (max value 2,147,483,647)? Maybe byte (max value 127) or short is good enough? byte is 8 times smaller than int.
  • Do you have really many identical values in array (like zeros)? Try to use sparse arrays.

for instance:

Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
map.put(27, new HashMap<Integer, Integer>()); // row 27 exists
map.get(27).put(54, 1); // row 27, column 54 has value 1.

They need more memory per value stored, but have basically no limits on the array space (you can use Long rather than Integer as index to make them really huge).

  • Maybe you just do not know how long the array should be? Try ArrayList, it self-resizes. Use ArrayList of ArrayLists for 2D array.

  • If nothing else is helpful, use RandomAccessFile to store your overgrown data into the filesystem. 100 Gb or about are not a problem in these times on a good workstation, you just need to compute the required offset in the file. The filesystem is obviously much slower than RAM but with good SSD drive may be bearable.

like image 33
Audrius Meškauskas Avatar answered Oct 21 '22 09:10

Audrius Meškauskas