File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max No of Elements in Array?

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
can anyone tell me..that is there any limit of adding elements in array..like as if i cannont add more that X elements in an array?

also what is the data type of the length field of the array...?
i didnt got anything relating to this anywhere..

Thanx in advance

Sandy
 
anand phulwani
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i solved one of your problem
length field returns an int.

This predicts the output


main.java:9: possible loss of precision
found : int
required: byte
byte x=a.length;
^
1 error
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i initilize an array with Integer.MAX_VALUE i get an error.



What is this error?
 
anand phulwani
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to look out for the error which says that its VM dependent, that is virtual memory dependent,i tried this code which gave me the following error


Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

so i decided to run a series of program code with the help of loop and find out ,the final code i ran was this which would be a bit clear



which gave me the following constant output when i ran this 4-5 times

15475321
15475322
15475323
15475324
15475325
15475326
15475327
15475328
15475329
15475330
15475331
15475332
15475333
15475334
15475335
15475336
15475337
15475338
15475339
15475340
15475341
15475342
15475343
15475344
15475345
15475346
15475347
15475348
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

and mentioning that my virtual memory is set to 1536-3072
if the program give same output on your machine that note that u can have it as 15475348 which i think will not be true,otherwise u can have this as VM dependent.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think it is int too. As far as I can see, the Java Language Specification mentions only a public final member length with no type specified. But in an example showing an equivalent class definition of an array, the length field is of type int. So that would mean a maximum length of an array is 2147483647 elements.

As an example, an array can be created as in the following:



The resultant initialized array of 42 zero (the default value) elements is fixed in length. You cannot add a 43rd element to it.

Of course there is a limit to the amount of memory available to the Java Virtual Machine, so for very large arrays you will probably get an OutOfMemoryError before the theoretical limit is reached.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can play with the following "switches" with the java command:
-Xms256m (here the starting heap size is 256 Megabytes)
-Xmx512m (here maximum heap size is 512 Megabytes)

I got an array of 20 000 000 int elements using -Xmx512m.
[ September 11, 2005: Message edited by: Barry Gaunt ]
 
anand phulwani
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So How Can We Know The Default Heap Size used By Our JVM.Is it size machine dependent or its constant for all.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explination...

Thanx for your replies guys.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic