• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java BufferedImage - OutOfMemoryError  RSS feed

 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Can any body me why am i getting the below exception? My Heap Size is 768MB. Actually i want to know how much Horizontal and Vertical dimension can java BufferedImage(..) method support. and how does this method work?

BufferedImage lvImage = new BufferedImage(60000, 2300, BufferedImage.TYPE_INT_RGB);


/ Nawaz

=================
CODE
=================



==================
EXCEPTION
==================



[Nitesh: Added code tags. Please UseCodeTags]
[ November 25, 2008: Message edited by: Nitesh Kant ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The BufferedImage class supports any image size you throw at it; the limiting factor is how much memory the JVM has available. The image consumes roughly 60000*2300*4 = 526MB; I can run the code using a maximum heap size of 600MB. How are you specifying the heap size? Does the JVM get allocated that much?

If you're interested in the inner working of BufferedImage, you can take a look at its source code. Somewhere in the JRE/JDK installation is a file called "src.zip" which contains the source code to all JRE classes.
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well at the maximum Java can allocate 256M memory to an Array. And i am getting below exception when i try to declare a big BufferedImage(..) object such as below;

=================
Code
=================


===================
Exception
===================




1. I am using IBM websphere 5.1 and under process definition i am declaring the Heap size like below;
Maximum Heap Size = 768.

2. i've also written a test class (posted in my first post) and i execute it using Xms command such as 'Xmx768m'.


================
My understanding
================
From the exception i could understand that 'Java can allocate 256M memory to an Array and BufferedImage also uses some Array in its internal mechanism'.


============
My Queries
============
1. Still i am unable to figure it out that how Heap size impect the declaration of BufferedImage Object?

2. And at what maximum horizontal/Vertical dimensions Java allow us to declare a BufferedImage Object?
 
Patel Chintan
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can increase size of JVM heap size by using xms Xmx parameters.
eg: -XX:MaxPermSize=128m -Xms128m -Xmx512m
[ November 26, 2008: Message edited by: Ulf Dittmer ]
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already increased dat heap size. this is not the issue..

Could you please answer to my queries?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the exception i could understand that 'Java can allocate 256M memory to an Array

Not Java itself - that's apparently a limitation of the IBM JVM. As I said, I have no problem running the code with a sufficiently large heap size on the Apple JVM. You could try the Sun JVM if that runs on your OS.

1. Still i am unable to figure it out that how Heap size impect the declaration of BufferedImage Object?

Constructing a BufferedImage object leads to an array being allocated that can hold all of the image. The heap size needs to be big enough to hold that array. The stack trace tells you in which classes and methods this happens - you can look at the source code if you're interested in the details.

2. And at what maximum horizontal/Vertical dimensions Java allow us to declare a BufferedImage Object?

There is no limit that BufferedImage imposes; see my first post.

But it begs the question: What are you trying to achieve by declaring a 60000 pixel wide image? Nobody is going to be able to view that.
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detailed reply...

Constructing a BufferedImage object leads to an array being allocated that can hold all of the image. The heap size needs to be big enough to hold that array. The stack trace tells you in which classes and methods this happens - you can look at the source code if you're interested in the details.


==========================================================
There is no limit that BufferedImage imposes; see my first post.
But it begs the question: What are you trying to achieve by declaring a 60000 pixel wide image? Nobody is going to be able to view that.


Ya you are right.. the image will not be readable on paper (printout).. However user can read it by using Zoom-IN.
Actually i need to create a dynamic image and i want to know the maximum dimensions so that i could avoid above Exception.. and should be able to show some Error message in this case that image is too large to be displayed....
==========================================================
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your users have software that can handle images with 138 Megapixels? I'm asking because a fair number of image viewers will be slow as molasses with that kind of image size, if they can handle it at all.

Knowing about the 256MB limit of the IBM JDK, you can calculate what image size is definitely too big using the same formula I used in my first post.
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
256MB = 268435456 Bytes
one pixel consume 4-bytes and this mean;

(Horizontal Dimension * Vertical dimension) = 268435456/4 = 67108864 pixels.

Queries
1. 67108864 should be the max value supported by IBM JDK?
2. what will be the impect of Heap Size in this case like what would happen if i set Heapsize= 512MB and likewise HeapSize=768MB?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since that error message doesn't mention the heap size (it only mentions a max array size) I would assume that this limit is independent of the heap size. But that's easy enough to test.
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM allocate memory from HEAP during Run time and i think if the required 256MB memory could not be allocated by JVM to create Big Image using BufferedImage(..) then it will throw some exception like below;




AND WITH THE BELOW LINE;





Thank you Ulf Dittmer, you've been really helpful!
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The image consumes roughly 60000*2300*4 = 526MB


Hello Dittmer, how did you calculate above value? Java BufferedImage.class tells that 'TYPE_INT_RGB' consumes 1 byte for one pixel??


Queries:
1. How much bytes will one pixel consume if i use the following line of code?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dittmer, how did you calculate above value? Java BufferedImage.class tells that 'TYPE_INT_RGB' consumes 1 byte for one pixel?

The name is Ulf. If you prefer to address me by my last name, then the polite thing to do is to prefix that by "Mr". But first name is fine.

No it doesn't say that. It says that the color components are packed into an integer, which in Java consumes 4 byte. Each of R, G and B is one byte alone; if there were an alpha component (like for TYPE_INT_ARGB) then that would occupy the 4th byte of that integer.
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for every thing which you did not like.. but that was really unintentional!

Thanks for the explanation Mr. Ulf :-)

Kind Regards,

Nawaz Ijaz
 
Nawaz Ijaz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ulf, i've one more query for you.

1. In your opinion which image type is more resource/memory hungry. PNG or JPG?

2. I shall be rendering large images on PDF via iText. so what image type would you suggest me in this case?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For photos I'd use JPEG, and for graphics PNG. I don't think I'd consider memory consumption when choosing image file formats; they're unlikely to make a big difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!