• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to overcome OutOfMemory exception?

 
Giorgos Christoulas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing a Java application for processing satellite images.
The size of every image is 2000*2000 pixels. When I try to set the pixel
values into an int array I get an OutOfMemory exception.
Here is a portion of my code:
PlanarImage originalImage = JAI.create("fileload",imagePath);
Raster originalRaster = originalImage.getData();
SampleModel originalSampleModel = originalImage.getSampleModel();
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int bands = originalRaster.getNumBands();
int length = width*height;
int originalPix[][] = new int[bands][];

for (int b =0;b<bands;b++)
originalPix[b] = new int[length];

for(int band = 0; band < bands;band++)
originalPix[band] = originalSampleModel.getSamples(0,0,width,height,band,originalPix[band],originalRaster.getDataBuffer());
Is there a limit on the number of elements an array can have?
I have set the memory heap size to 160MB (-J -Xmx160m) but the results are the same.
Note that this part of the code will be implemented in a GUI
Does anyone knows how to overcome this problem?
Thanks in advance
Giorgos
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OutOfMemoryError isn't an exception should not be caught, for what I know, you need more memory...
 
Giorgos Christoulas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leonardo. The physical memory of my system is 1024MB.
I am sure that physical memory is not the problem.
My suspicion is that the problem arises at the point where I try to allocate memory for an one dimensional array of 2000*2000(=4000000) elements. Theoretically there should be no problem but apparently there is.
Unfortunately I don�t know how to overcome it.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The limit on array size is huge.
What happens if you double the -Xmx setting?
Bill
[ March 02, 2004: Message edited by: William Brogden ]
[ March 02, 2004: Message edited by: William Brogden ]
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does -Xmx do?
 
Chris Hall
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leandro oliveira:
what does -Xmx do?

Execute 'java -X' and you will see all the options.
The -Xmx<size> will set the maximum java heap size to that specified. This will allow the java vm to use the amount of memory specified, assuming the OS has it available. Increasing the size can typically solve out of memory problems.
Example: java -Xmx130M MyClass will create a virtual machine and allow up to 130MB to be used.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you know what is the default value?
 
Chris Hall
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/hotspot/ism.html
Link states: The default maximum size is 64MB
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to get a memory profiler to monitor the object size when you run the application. 160MB seems quite big already for your 4MB sized image. But a profiler will certainly help identifying the problem.
 
Parth Sagdeo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I'm pretty sure that in real life, a 2000 x 2000 pixel pic is probably going to be around 150-200 megabytes, believe it or not, its definately NOT going to be 4 mb though
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GC: My suspicion is that the problem arises at the point where I try to allocate memory for an one dimensional array of 2000*2000(=4000000) elements. Theoretically there should be no problem but apparently there is.
Ok, let's see:

Output: two-dimensional array 2000x2000 took 15Mb of memory.
So, it looks like your problem is somewhere else. I would suggest running your app with a -verbose:gc parameter passed to JVM, -- that may give you more information. At least you would be able to see if your -Xmx and -Xms parameters are recognised by the JVM. Are you by chance starting your program from an Ant script? I found a few days ago that under some circumstances, it limits the memory heap to around 16M, no matter what params you pass.
[ March 21, 2004: Message edited by: Eugene Kononov ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2000x2000 pixels = 4 million pixels. As each takes 3 bytes, that's 12 million bytes.
Machine hasa 4 byte boundary though between array elements so each element takes 4 not 3 bytes leading to 16 million bytes being consumed.
That's 15.26MB of memory for just the raw pixel data (and that's when using a 24 bit image...).
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't Java always use 4 bytes per pixel in RGB representation with the 4th byte the Alpha (opacity) channel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic