• Post Reply Bookmark Topic Watch Topic
  • New Topic

NegativeArraySizeException  RSS feed

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i run this program,i got the "NegativeArraySizeException" ,please tell me How can i reduce this?
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The size of an array can not be larger than http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE

Everything else you need to know can be found at the web site where that code was copied:
http://introcs.cs.princeton.edu/java/14array/HugeArray.java.html
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The maximum length of an array in Java can be Integer.MAX_VALUE. Even if you try to declare an array which is that large in size, chances are that you'll get OutOfMemoryError - a single such array will take approx. 4.3GB of memory.

Apart from that, I'm just thinking - what kind of application requires such a huge array (having length of 1 trillion - which will take 1TB of memory)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:The maximum length of an array in Java can be Integer.MAX_VALUE. Even if you try to declare an array which is that large in size, chances are that you'll get OutOfMemoryError - a single such array will take approx. 4.3GB of memory.

Apart from that, I'm just thinking - what kind of application requires such a huge array (having length of 1 trillion - which will take 1TB of memory)


Actually, each int element takes 4 bytes, so such an array will require (in the ballpark range of) 4 times the 1TB of memory....

Henry
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes

It will take 4TB of memory. In real life, I've never seen a machine with 4TB of RAM (rather this code will need 4TB of 'JVM' itself)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have all been on about arrays too large. But the question was not about arrays too large. It was about arrays too small. I know exactly why it was too small, but does Slaxmi Raj?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:We have all been on about arrays too large. But the question was not about arrays too large. It was about arrays too small. I know exactly why it was too small, but does Slaxmi Raj?

He does if he followed the Princeton link, and read and understood the source of the unattributed code he copied and pasted in his post.
 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for your explanation. Iam beginer in java, this is competitive question.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is competitive, you ought to produce an answer.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Yes

It will take 4TB of memory. In real life, I've never seen a machine with 4TB of RAM (rather this code will need 4TB of 'JVM' itself)

It doesn't need 4TB of physical RAM - the OS can use swap files for the data that does not fit into memory. However, this will significantly decrease performance as the disk will be accessed quite a bit for the swapping.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Anayonkar Shivalkar wrote:Yes

It will take 4TB of memory. In real life, I've never seen a machine with 4TB of RAM (rather this code will need 4TB of 'JVM' itself)

It doesn't need 4TB of physical RAM - the OS can use swap files for the data that does not fit into memory. However, this will significantly decrease performance as the disk will be accessed quite a bit for the swapping.


"significantly decrease performance" is an understatement. The garbage collector and the swap file system are, in my opinion, mortal enemies. The job of swap is to put the least recently used pieces of memory out to disk. And the garbage collector's job is to find the garbage, which generally are the least recently used pieces -- not to mention that it needs to traverse all of memory (as the Sun/Oracle implementation does GC when the heap is full).

IMO, never ever run a JVM with swapped memory !!!

Henry

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!