Java BufferedImage Memory Consumption

Our application generates images. The memory consumed BufferedImagethrows an exception in memory:

java.lang.OutOfMemoryError: Java heap space

This happens with the following line:

BufferedImage result = new BufferedImage(2540, 2028, BufferedImage.TYPE_INT_ARGB);

Checking the free memory immediately before this instruction, it shows that I have 108 MB of free memory. The approach I use to test memory is:

Runtime rt = Runtime.getRuntime();
rt.gc();
long maxMemory = rt.maxMemory();
long usedMemory = rt.totalMemory() - rt.freeMemory();
long freeMem = maxMemory - usedMemory;

We do not understand how it BufferedImagecan consume more than 100 MB of memory. It should use 2540 * 2028 * 4 bytes, which is ~ 20 MB.

Why is so much memory consumed during creation BufferedImage? What can we do to reduce this?

+4
2

, , . , maxMemory - usedMemory, , , VM , - , - , - , .

, 108 , 20 . BufferedImage, , int [], . , 20MB-, , , OutOfMemoryError. - GC ; .

- , , GC ( VM ), .


: , GC ( Java 7 (JDK 7) G1) - , , . . GC , .

, 900 , 100 , - - , , 2 . , GC (, GC) - , , . , GC.

+2

BigBufferedImage BufferedImage. BigBufferedImage , . 2,147,483,647 ( 46,340 x 46,340 ).

az BigBufferedImage:

    BigBufferedImage image = BigBufferedImage.create(
            tempDir, width, height, type);

BigBufferedImage:

    BigBufferedImage image = BigBufferedImage.create(
            imagePath, tempDir, type);

:

    part = image.getSubimage(x, y, width, height);

.

0

All Articles