This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You're trying to instantiate an array that can hold over 2.1 billion int values. At 16 bits each, that's nearly 4.3 GB. It's little wonder you ran out of memory.
(PS: You have the 4 and the 6 reversed. The max int value is 2147483647.)
EDIT: Of course, ints are 32 bits each. (Whoops.) See correction below. [ March 28, 2006: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Jan 05, 2006
Hi, Thanks for the reply.But still i don't get this.May be i am stupid.Better explain [int dimension limit] [what is long dimension],if we give long dimension in array ,what would be the result. Thanks in advance.
The program is attempting to allocate space in memory for over 2.1 billion ints. Each int is 4 bytes long which is 32 bits. 32*2.1 billion is about 67.2 billion bits which is somewhere over 64 GB. This is far more memory that you will probably have on your system.
If you use
you can find out how much memory that your JVM can use.
You're trying to create an array of ints. For each position in the array you will need 32 bits (not 16) to store the int because the JLS defines the exact size of an int. So, for each int in your array you'll need four bytes. 2147483647 * 4 is 8589934588. Does your system have 8589934588 bytes of memory? No, of course not, so the JVM cannot possibly create an array that big and when you try to it generates an OutOfMemory error because you are quite literally out of memory.
Joined: Jul 15, 2003
Keith, it's 8GB not 64GB. Regardless, they don't have the memory.
Using a long value to specify an array size results in a compiler error unless the value is explicitly cast to type int. In general, the value specifying the array size must be a type that is "int convertable" (can be widened to type int without explicit cast).
The type of each dimension expression within a DimExpr must be a type that is convertible (�5.1.8) to an integral type, or a compile-time error occurs. ...this means, specifically, that the type of a dimension expression must not be long.
But not all int values are valid for specifying an array size. For example, a negative value will result in an NegativeArraySizeException.
Remember, when an object is created (and arrays are objects), the memory for that object is allocated. So if the size of the array is too large, then the OutOfMemoryError will occur.