0

I have tried the following way:

int[][] Matrix=new int[Integer.MAX_VALUE-5][Integer.MAX_VALUE-5];

But I get this exception: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.

What i mean is i want to store a matrix of nxn size where n is around 500000, but my Netbeans shows out of memory exception for just 10000, how would i increase the memory

3 Answers3

4

There is no way that array will fit in memory.

Integer.MAX_VALUE is 2^31.

Two dimensions means storing 2^62 integers at 4 bytes each.

Thilo
  • 257,207
  • 101
  • 511
  • 656
2

You should have a look at a sparse matrix, which allows you to process huge matrices containing only relatively few values.

Examples of a sparse matrix format:

  • CSR (Compressed sparse row) -- stores non-zero elements in contiguous array with another two arrays containing column indices and pointers to row beginnings
  • COO (Coordinate list) -- stores array of tuples (row, column, value)
Erbureth
  • 3,378
  • 22
  • 39
1

For 10000 x 10000 set your heap size to 512MB: java -Xmx512m. For 500000 x 500000 set your heap size to about 1TB: java -Xmx1000000m. You might want to wait a few years for computers with that amount of memory to become available for the general public...

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29