wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java array memory allocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java array memory allocation" Watch "Java array memory allocation" New topic
Author

Java array memory allocation

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Guys,

What will happen when you do


Or in other words can we allocate such large arrays.

Thanks,
Martin
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If there's enough memory available, yes. You may have to use Sun's -Xmx switch (or another vendor-specific mechanism) to set the max size of the Java heap.


[Jess in Action][AskingGoodQuestions]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi,

The last I remember I had a question along similar lines and Jim Yingst pointed that as array object have length field which is of type 'int' we can only create arrays of len Integer.MAX_VALUE. Again this is true only if we have sufficient memory to create Integer.MAX_VALUE size array.

I ran the following,


and got,
java.lang.OutOfMemoryError
Exception in thread "main"

Regards
Maulin
[ March 04, 2005: Message edited by: Maulin Vasavada ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Maulin Vasavada:
byte[] b = new byte[Integer.MAX_VALUE];
Yes well, not everyone has 2GB of RAM devoted exclusively to the JVM!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java array memory allocation
 
Similar Threads
Setting buffer size
Create a 4GB string?
nio performance issue
creating blocks of raw bytes
Reading large files in Java