Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java array memory allocation

 
Martin Lira
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic