aspose file tools*
The moose likes Performance and the fly likes How to overcome OutOfMemory exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "How to overcome OutOfMemory exception? " Watch "How to overcome OutOfMemory exception? " New topic
Author

How to overcome OutOfMemory exception?

Giorgos Christoulas
Greenhorn

Joined: Sep 24, 2003
Posts: 13
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

Joined: Nov 07, 2002
Posts: 298
OutOfMemoryError isn't an exception should not be caught, for what I know, you need more memory...
Giorgos Christoulas
Greenhorn

Joined: Sep 24, 2003
Posts: 13
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

Joined: Mar 22, 2000
Posts: 12823
    
    5
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

Joined: Nov 07, 2002
Posts: 298
what does -Xmx do?
Chris Hall
Ranch Hand

Joined: Dec 04, 2002
Posts: 39
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

Joined: Nov 07, 2002
Posts: 298
do you know what is the default value?
Chris Hall
Ranch Hand

Joined: Dec 04, 2002
Posts: 39
http://java.sun.com/docs/hotspot/ism.html
Link states: The default maximum size is 64MB
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
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.


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Parth Sagdeo
Ranch Hand

Joined: Mar 18, 2004
Posts: 40
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Oct 12, 2000
Posts: 5093
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...).


42
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Doesn't Java always use 4 bytes per pixel in RGB representation with the 4th byte the Alpha (opacity) channel.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to overcome OutOfMemory exception?