Search code examples
javamultidimensional-arraymemory-limit

Memory limit exceeded with the use of a 2D array


This line of code creates an error message "Memory limit exceeded" when length is 100,000 and I don't quite understand why.

int[][] multiply = new int[length][length];

Solution

  • You try to make an array of 100,000 * 100,000 entries of probably 4 bytes each.
    That is 40,000,000,000 bytes, around 40GB. Even if you have that kind of memory installed, the system will probably enforce some limits long before that.