Admittedly, I could have figured it out through trial and error, but I would also like to know whether or not this number varies and how (computer performance, other data structures present, compiler type, etc.). Thanks!
-
4Yes, it varies greatly :-) However, the maximum *number of elements* (absolute) of an array is [2^31-1] (max of `int` which is `Integer.MAX_VALUE`). The *absolute* maximum size of other data-structures may follow suit if they use arrays or `int`, etc. However, most [consumer] computers won't [happily] process even a max-element array of any kind simply due to memory requirements :-) It would require 16GB+ (4bytes x 4B) (and count in GC mark-and-sweep, so perhaps 32GB+?) for `int[2^32-1]`. At the very least this would require a 64-bit VM just for the address space. – Dec 29 '10 at 01:25
-
The maximum number of elements is not `Integer.MAX_VALUE`, it's actually `Integer.MAX_VALUE - 5`. See: http://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size – Flimm Mar 12 '13 at 14:43
3 Answers
You will need to increase you JVM heap size if you run out of memory, read this. Nothing you can do if your matrix requires a lot of memory (if there is no memory leaks) other than increasing the heap size.
You can change the size of your matrix as large as you want (but not bigger than the maximum value of integer which is used as index) if you have enough memory. Integer is 32bits so, you have the maximum theoretical limit there.

- 8,879
- 7
- 35
- 51
While the maximum array size is the limited by a 32-bit signed value. i.e. 2^31-1 or about 2 billion, most matrices are implemented as two dimensional arrays so the maximum size is 2 billion * 2 billion. You could use float or double, but if you had a matrix that big your accumulated rounding error would be enormous. ~ 2^62 bits which is more than the accuracy of a double, so you would have to use BigDecimal in any case. Say the each cell took about 128 (2^7) bytes of memory you would need a total of 2^69 bytes or 512 Exa-bytes (32x the theoretical limit of the memory a 64-bit processor can handle)

- 525,659
- 79
- 751
- 1,130
it also depends upon the memory of your machine and how much memory you allocate for the process using -Xmx.

- 462
- 3
- 13