Skip to content
Advertisement

“java.lang.OutOfMemoryError: Java heap space” in image and array storage

I am currently working on an image processing demonstration in java (Applet).

I am running into the problem where my arrays are too large and I am getting the “java.lang.OutOfMemoryError: Java heap space” error.

The algorithm I run creates an NxD float array where: N is the number of pixel in the image and D is the coordinates of each pixel plus the colorspace components of each pixel (usually 1 for grayscale or 3 for RGB). For each iteration of the algorithm it creates one of these NxD float arrays and stores it for later use in a vector, so that the user of the applet may look at the individual steps.

My client wants the program to be able to load a 500×500 RGB image and run as the upper bound. There are about 12 to 20 iterations per run so that means I need to be able to store a 12x500x500x5 float in some fashion.

Is there a way to process all of this data and, if possible, how?

Example of the issue: I am loading a 512 by 512 Grayscale image and even before the first iteration completes I run out of heap space. The line it points me to is:

Y.add(new float[N][D])

where Y is a Vector and N and D are described as above. This is the second instance of the code using that line.

EDIT: The upper bound, as I mentioned but forgot to correct should be around: 20+ x500x500x5 (20 iterations, 500 width, 500 height, 5 dimensions (where 3 comes from RGB and 2 for the coordinates (The coordinates move and so do the pixels, so I need to record the values, which can and are decimals)) (Approx. 100000000 bytes)

Advertisement

Answer

I don’t think you are going to be able to do this as an Applet. (There may be settings in the browser for how much memory to assign to applets, but I’m not aware of any.)

If you can use Java webstart, I would use that. You can set the amount of memory required:

<j2se version="1.4+" initial-heap-size="100M" max-heap-size="200M"/>

The advantage with using webstart is that the user does not need to configure their browser.

EDIT: The Java control panel does have memory settings for applets. See How can I start an Java applet with more memory?

However, unless you have just a handful of clients who don’t mind altering their java settings (and that they have appropriate permissions and skills), this is not a nice user experience. With web start,you can set all this up in the JNLP descriptor and you’re all set to go.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement