This week's book giveaways are in the iOS and Features new in Java 8 forums. We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line! See this thread and this one for details.
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
OutOfMemoryError isn't an exception should not be caught, for what I know, you need more memory...
Joined: Sep 24, 2003
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.
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.
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
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 ]
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...).
Author and all-around good cowpoke
Joined: Mar 22, 2000
Doesn't Java always use 4 bytes per pixel in RGB representation with the 4th byte the Alpha (opacity) channel.