File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what should be maximum size of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what should be maximum size of an array " Watch "what should be maximum size of an array " New topic
Author

what should be maximum size of an array

Uma Kanth
Greenhorn

Joined: Sep 16, 2002
Posts: 20
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

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Uma Kanth
Greenhorn

Joined: Sep 16, 2002
Posts: 20
Does that mean that it is System dependent?
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401

Anything bigger and I get an OutOfMemoryError.
Is there anything practically related btn the number 14902025 and my error?


comp.lang.javascript FAQ: http://jibbering.com/faq/
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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).


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what should be maximum size of an array