"java.lang.OutOfMemoryError: Java heap space" in the image and array storage

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

I ran into a problem when my arrays are too large and I get the error message "java.lang.OutOfMemoryError: Java heap space".

The executed algorithm creates an NxD floating-point array, where: N is the number of pixels in the image, and D is the coordinates of each pixel plus the color space components of each pixel (usually 1 for grayscale or 3 for RGB). For each iteration of the algorithm, it creates one of these NxD floating point arrays and stores it for later use in the vector so that the applet user can look at the individual steps.

My client wants the program to be able to load an RGB 500x500 image and work as an upper bound. About 12 to 20 iterations per turn, so I should be able to store the 12x500x500x5 float in some way.

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

An example of a problem: I upload an image ranging in size from 512 to 512 in shades of gray, and even before completing the first iteration, I run out of empty space. The line to which she points is as follows:

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

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

EDIT: the upper border, as I mentioned, but forgot to fix, should be approximately: 20+ x500x500x5 (20 iterations, 500 width, height 500, 5 dimensions (where 3 is RGB and 2 for coordinates (moving coordinates and also pixels, so I need to write down values ​​that can and are decimal)) (Approximately 100,000,000 bytes)

+4
source share
4 answers

I don’t think you can do it like an applet. (The browser may have settings for how much memory will be assigned to the applets, but I do not know about them.)

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

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

The advantage of using webstart is that the user does not need to configure his browser.

EDIT: The Java control panel has memory settings for applets. See How to run a Java applet with a large amount of memory?

However, if you do not have only a few clients who do not mind changing their java settings (and that they have the appropriate permissions and skills), this is not a very nice user interface. With a web launch, you can install all of this in the JNLP descriptor, and you're ready to go.

+3
source

Have you tried increasing the size of the initial heap, as well as setting the maximum heap size?

 java -Xms<initial heap size> -Xmx<maximum heap size> 

eg.

 java -Xms256M -Xmx512M 

The defaults are pretty conservative and small, so you definitely want to try and play around with the heap size settings.

-1
source

1) 12x500x500x5 floats mean about 60 MB, as well as some overhead. You should have no problem reserving all this memory space on a smart machine if you configure your JVM heap correctly (e.g. -Xmx500M). If, on the other hand, you run it as an applet, you have to make some settings (I found this link, for example, related to updating Java 6 10 and above).

2) You did not provide much information regarding the data format per pixel. Perhaps you can make it more compact. Why do you need 5 floats per pixel? You can save the RGB value as 3 bytes, and it looks like you don’t need to save the location of the pixel, since the position in the 2-dimensional array already indicates the position.

-1
source

To increase the heap space size beyond 64 MB by default, you need to run the application with the -Xmx parameter (for the applet, add it to the Java runtime parameters that can be found in the administration tools on any OS). I guess this is not an option if it is a web application where you have to modify many end computers. I would like to know more about this repository in an x-dimensional array. What exactly do you store? Why 4 bytes float? The uncompressed source image is usually stored in a WID thank you HEIGHT array, where each cell contains a colored object (for example, 3 shorts describing the RGB space). Such an array for a 500x500 image will require 500 * 500 * 3 (the number of color dimensions) * 2 (bytes for a short one) = 1,500,000 bytes, less than 500 * 500 * 5 (it’s still not clear why 5, maybe you store color + coordinates) * 4 (bytes in float) = 5,000,000 bytes. Keep in mind that it is for this reason that compression algorithms (such as JPEG) were developed, so you do not need to store 1.5 MB in memory for each image. As for iterations, but mainly for their subsequent storage, there is nothing to say if you do not want to get into the hassle of writing something like the MPEG format, where you will need to save changes only between frames.

-1
source

Source: https://habr.com/ru/post/1311771/


All Articles