2

I got to find from Do Java arrays have a maximum size? that there is a maximum limit for array size .

May be with the intention that it might deplete heap space . Yes, I agree with the point .

I cannot understand the following :

  1. But why to have this limit for every single array ?

  2. what if I have a number of arrays of such large size ?

  3. why not throw an exception when some threshhold of heap space is reached overall(total consumption) , instead of having upperbound for each array ?

Note :

  1. In Python, they have this limit How Big can a Python Array Get?

  2. In C , there seems like no limit (except the hardware used) The maximum size of an array in C

Community
  • 1
  • 1
Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
  • 2
    Nothing to do with heap space. All to do with using a 32-bit integer for addressing. – Dawood ibn Kareem May 14 '14 at 04:13
  • And that the array space must be contiguous. It may be relatively easy to allocate a 2GB array when the JVM starts up, but in a long-running application with a fragmented heap (depending on your GC implementation) it may be nearly impossible. – William Price May 14 '14 at 04:25
  • But why ? had the array been designed with index being a still more larger range this would have been possible ? may I know why the silly index the limiting factor for such a feature . to be practical its like saying "because the person's name has only six characters he can study only till 6th standard "from textbooksonline.tn.nic.in/Books/11/Std11-CompSci-EM-2.pdf page 257 I am getting a sense that only address matters and index is just used to calculate next address from starting address . so why is this index given so much importance and serves as a limiting factor for size of array. – – Harish Kayarohanam May 14 '14 at 05:19
  • I found this one http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c – Harish Kayarohanam May 14 '14 at 05:23
  • Fun corollary: all of `ArrayList`'s operations are actually O(1), even though we think of them as O(N). For instance, `contains(Object)` is O(1) because I can come up with a constant (2^31-1) which is a JLS-enforced ceiling for how many steps it'll take (with some constant multiplier). :) (But don't say it's O(N) on any test or in an interview!) – yshavit May 14 '14 at 06:01

3 Answers3

5

Arrays in java are indexed by int values. So even if you have infinte memory, the max no. of elements an array can hold is 2^31-1 (Please correct me if I am wrong with the number... but you get the idea).

However, your memory will limit you on how many such large arrays you can keep in your heap...

Hirak
  • 3,601
  • 1
  • 22
  • 33
3

The length of an array is an int, so you can't allow arrays with more than Integer.MAX_VALUE elements without breaking length. Breaking length would be huge; it would require source changes and recompilation all over the place.

Given that you already have a length cap mandated by the length field, there's not a huge difference between a cap of Integer.MAX_VALUE or something like Integer.MAX_VALUE - 5.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Oh Thanks.But why ? had the array been designed with index being a still more larger range this would have been possible ? may I know why the silly index the limiting factor for such a feature . to be practical its like saying "because the person's name has only six characters he can study only till 6th standard ". – Harish Kayarohanam May 14 '14 at 05:15
  • from http://www.textbooksonline.tn.nic.in/Books/11/Std11-CompSci-EM-2.pdf page 257 I am getting a sense that only address matters and index is just used to calculate next address from starting address . so why is this index given so much importance . – Harish Kayarohanam May 14 '14 at 05:18
  • 1
    @HarishKayarohanam: Making the length a long would require unwieldy casts and two extra, generally-unused bytes for all arrays. – user2357112 May 14 '14 at 05:35
  • @HarishKayarohanam may I ask you why would you use an array with 2147483647 elements inside it? Do you have such real world use case or is it pure curiosity? – Luiggi Mendoza May 14 '14 at 05:36
  • No I was reading datastructures and slowly started to go inside and explore things and I found this limiting factor and was puzzled .Could not think of a reason , so raised it here .Did you see this link http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c . – Harish Kayarohanam May 14 '14 at 05:38
  • @user2357112 : why is the index range going to create unused bytes ? it is just for calculation and implementation pupose right . it is finally only the starting address that is stored (I have quoted a reference in comment 2 a book). They have index to deduce memory address of some other element in array as starting_address + (size of each element)*index – Harish Kayarohanam May 14 '14 at 05:40
  • @HarishKayarohanam no, but it is good to know for curiosity purposes. In real world applications, you would not use such enormous arrays, I guess that was the motivation for James Gosling to design arrays in Java to have at most an `int` maximum value as the maximum length value for arrays. – Luiggi Mendoza May 14 '14 at 05:41
  • Ya this answer sounds meaningful . It was just a design decision and because of its lack of usefulness it was designed this way. – Harish Kayarohanam May 14 '14 at 05:42
  • @HarishKayarohanam: If you make the length a long, you need to store an 8-byte long instead of a 4-byte int. You're not just storing the array's starting address (in fact, the array's address isn't part of the array at all; it's the cost of the reference to the array). – user2357112 May 14 '14 at 05:48
0

Lets say if you have a array

String[] array = new String[22L];

Error message in eclipse

Type mismatch: cannot convert from long to int 

Clearly tells that the size of array takes int for length which is limited by ** max int value which is limited by 32-bits**

vkg
  • 1,839
  • 14
  • 15
  • Try adding an L at the end. – Luiggi Mendoza May 14 '14 at 04:19
  • You can not do that. Because arrays take int as their size. in that case the error message eclipse will show is Type mismatch: cannot convert from long to int – vkg May 14 '14 at 04:20
  • Then the second error message is the proper explanation for this reason. – Luiggi Mendoza May 14 '14 at 04:21
  • Try your self you cannot create a array String[] array = new String[1L]; if that explains :) – vkg May 14 '14 at 04:24
  • Again: your second explanation (in comments) is the right one. The explanation in your current post **it is not**. – Luiggi Mendoza May 14 '14 at 04:25
  • By the way, your error message refers to literal integers, not to the size of the array nor the type of the size of the array (so your explanation does not explain why the array takes `int` as its size). – Luiggi Mendoza May 14 '14 at 04:27
  • And that error message is clear enough that max size of arrays is limited by max int value. Because arrays dont take long etc as their size. Strange you still insist. I am not even talking about type of array. – vkg May 14 '14 at 04:31
  • It is not. That error message is for trying to store a value bigger than an int inside an int. – Luiggi Mendoza May 14 '14 at 04:36