aspose file tools
The moose likes Beginning Java and the fly likes NegativeArraySizeException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "NegativeArraySizeException" Watch "NegativeArraySizeException" New topic
Author

NegativeArraySizeException

Slaxmi Raj
Ranch Hand

Joined: Apr 20, 2012
Posts: 40
when i run this program,i got the "NegativeArraySizeException" ,please tell me How can i reduce this?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Jan 28, 2008
Posts: 5575

Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1295

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)


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16811
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1295

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
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Apr 20, 2012
Posts: 40
thank you so much for your explanation. Iam beginer in java, this is competitive question.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
If it is competitive, you ought to produce an answer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16811
    
  19

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NegativeArraySizeException
 
Similar Threads
Have resolve symbol problems with a constructor. Please help.
Overriding Vs. overloading
power () and cube()
anilbachi question..
Parsed Batch update