Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what should be maximum size of an array

 
Uma Kanth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us say
byte b[] = new byte[n];
or
int i[] = new int[n];
What is the maximum permitted value of n in both the above cases?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since n must be an int (JLS 15.10 Array Creation Expressions), the maximal (theoretical) length of an array is Integer.MAX_VALUE, i.e. 2147483647. I say theoretical because when I run the following program, I get an OutOfMemoryError which means that there was not enough memory to run the program.
 
Uma Kanth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean that it is System dependent?
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anything bigger and I get an OutOfMemoryError.
Is there anything practically related btn the number 14902025 and my error?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number is a little less than one fourth of the number of bytes available on the stack (64 MB) - which makes sense, as each int takes four bytes. Try varying the memory allocated to the stack by running with -Xmx:
java -Xmx128m MyClass
This allocates up to 128MB to the stack (assuming your system has it available, of course).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic