Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Maximum Size?

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Does anyone know if java places a maximum on the number of elements an array can potentially have?

Thanks
[ June 02, 2007: Message edited by: Sam Bluesman ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see here
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm... using the example, in the topic, that Fred referenced. It looks like there is a (slightly below) 2 gigabyte size limit for integer arrays.

For example, when I try this...


int[] j = new int[Integer.MAX_VALUE/4];


I get an out of memory condition. However, when I do this...



It works fine, even though this second case allocates more memory.

BTW, I used a "-Xmx20g" switch for the heap, so the out of memory condition is not due to the heap size.

Henry
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW there are switches when you start java to specify thay you need more
memory. eg. java -Xms<size> <main-class>
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After playing around for a bit, it looks like the maximum array size is slightly less than 2 gigabytes -- for all array types. I can have almost twice as many elements of shorts than ints, twice as many elements of bytes than shorts, etc.

I don't have the exact number for elements, but it's close enough. And again, I used a very large heap, so the out of memory condition is not an out of heap memory condition.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic