Maximum limit for Java array

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

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

but it does not work 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 problem is to increase the JVM memory, but I'm trying to provide code for an online encoding request. There it also fails, and I cannot change the settings there.

Is there any standard limit or guide for creating large arrays that should not exceed?

+6
source share
5 answers
int[][] adjecancy = new int[96295][96295];

When you do this, you try to isolate the bits 96525*96525*32, which are almost 37091MB, which is about 37 gigs. It is very impossible to get memory from a PC just for Java.

, . , ArrayList, , - .

. , . , , JVM .

+13

, , . , :

  • int ( 2,147,483,647)? byte (max 127) short ? byte 8 int.
  • (, )? .

:

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.

, ( Long, Integer , ).

  • , , ? ArrayList, . ArrayList ArrayLists 2D-.

  • , RandomAccessFile, . 100 Gb , . , , , , SSD .

+3

, , 1/4- .

1 int Java 4 , 37,09 .

, , , 148 . .

.

: http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html

, .

+1

JVM . int 4 . , 1 , 1024 * 256 (1 = 1024 * 1024 ). , 2D- .

0

, , JVM.

96295 * 96295 * 4 ( ) = 37,090,908,100 = ~ 34,54 . JVM . .

, - -Xmx:

    Scanner scanner = new Scanner(System.in);
    while(true){
        System.out.println("Enter 2-D array of size: ");
        size = scanner.nextInt();

        int [][]numbers = new int[size][size];
        numbers = null;
    }

. -Xmx 512M → 2- ~ 10k +.

Typically, most online judges have a ~ 1.5-2GB bunch when evaluating applications.

0
source

Source: https://habr.com/ru/post/1016380/


All Articles