Home arrow static arrow Java Programming [Archive] - OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?
5 Duke Stars available
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:26
Registered: 4/15/03
OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 8:49 AM



 
Hello,

I am getting an OutOfMemoryException when I am initializing a new array of bytes:

byte[] barr = new byte[Integer.MAX_VALUE];

Why?

Thanks,

Corrine
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 8:52 AM (reply 1 of 16)



 
What in the blue blazes do you plan on doing with that array?
 

Posts:26
Registered: 4/15/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 8:57 AM (reply 2 of 16)



 
Well, I am dealing with 32-bit values, and each element of the array corresponds to one of two options for each bit. Thus, the number of elements is 2^32, which is clampled at Integer.MAX_VALUE.

What in the blue blazes do you plan on doing with that
array?
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:04 AM (reply 3 of 16)



 
Well, I am dealing with 32-bit values, and each
element of the array corresponds to one of two options
for each bit. Thus, the number of elements is 2^32,
which is clampled at Integer.MAX_VALUE.

??

I don't understand that reasoning at all. You're using a whole byte to signify whether a bit is on or off in a 32-bit number? While that seems extraordinarily wasteful, I still don't see how you extrapolate that you need 2^32 elements...
 

Posts:9,091
Registered: 8/15/01
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:08 AM (reply 4 of 16)



 
I am getting an OutOfMemoryException when I am

well, at a guess you are running out of memory, give it more with
java -Xmx <a large number here> <app>

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
 

Posts:1,906
Registered: 97-08-23
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:10 AM (reply 5 of 16)



 
I'm with yawmark on thinking this is not the best way to approach your problem. But the answer to your question is, you're asking the JVM for 2 gigabytes of memory for that array - and you don't have that much available. Don't do that.

Grant
 

Posts:5,965
Registered: 5/17/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:12 AM (reply 6 of 16)



 
Well, I am dealing with 32-bit values, and each
element of the array corresponds to one of two options
for each bit. Thus, the number of elements is 2^32,
which is clampled at Integer.MAX_VALUE.

Integer.MAX_VALUE is (2^31) - 1.

You get the out of memory exception because the array is allocated on the heap which has a default value of 64 MByte.

Say you have a possible value range from 0 to 2^32 but the actual number of values are (much) fewer, then you're better off using a hash table. It has close to constant access just like an array.
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:13 AM (reply 7 of 16)



 
Well, I am dealing with 32-bit values, and each element of the array corresponds to one of two options for each bit.

Why are you choosing to do this?
 

Posts:26
Registered: 4/15/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:50 AM (reply 8 of 16)



 
Well, I am dealing with 32-bit values, and each
element of the array corresponds to one of two options
for each bit.

Why are you choosing to do this?

Because the array is supposed to be a grayscale map for a 32-bit image.
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 9:55 AM (reply 9 of 16)



 
Well, I am dealing with 32-bit values, and each
element of the array corresponds to one of two
options
for each bit.

Why are you choosing to do this?

Because the array is supposed to be a grayscale map
for a 32-bit image.

Okay, maybe my question wasn't clear enough. Why have you set your requirements to use a byte array in which each byte serves to represent a single bit as a representation of a grayscale map of a 32-bit image? Can you post some code? Why have you chosen to allocate ~2GB of RAM to work with a single image?

There has got to be a better way to accomplish what you're trying to do.
 

Posts:18,384
Registered: 21.03.00
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 28, 2004 10:08 AM (reply 10 of 16)



 
Hmm... but isn't it true that you only need width * height number of ints if you want to hold a 32 bit value for each pixel in the image.

I can't come up with one reason to define a byte array of length Integer.MAX_VALUE

/Kaj
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 29, 2004 6:52 AM (reply 11 of 16)



 
I can't come up with one reason to define a byte array
of length Integer.MAX_VALUE

/Kaj

I'm assuming the lack of response from the OP indicates she(?) is having a tough time coming up with a reason as well... :o)
 

Posts:26
Registered: 4/15/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 29, 2004 8:01 AM (reply 12 of 16)



 
Well, I am dealing with 32-bit values, and each
element of the array corresponds to one of two
options
for each bit.

Why are you choosing to do this?

Because the array is supposed to be a grayscale map
for a 32-bit image.

Okay, maybe my question wasn't clear enough. Why have
you set your requirements to use a byte array in which
each byte serves to represent a single bit as a
representation of a grayscale map of a 32-bit image?
Can you post some code? Why have you chosen to
allocate ~2GB of RAM to work with a single image?

There has got to be a better way to accomplish what
you're trying to do.

The answer to your question is because as I understand it, the structure of the grayscale map that I'm using corresponds to the required specification in defining an associated IndexColorModel.

 

Posts:403
Registered: 9/4/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 29, 2004 11:39 AM (reply 13 of 16)



 
And who wrote that specification? Kingston?
 

Posts:24,036
Registered: 2/3/03
Re: OutOfMemoryError on new byte[Integer.MAX_VALUE]. Why, how to fix?  
Jul 29, 2004 11:41 AM (reply 14 of 16)



 
The answer to your question is because as I understand
it, the structure of the grayscale map that I'm using
corresponds to the required specification in defining
an associated IndexColorModel.

For some reason, I think it may be a good idea for you to revisit your understanding! :o)
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »