简体   繁体   中英

High memory consumption with BufferedImage object

We have been using BufferedImage objects in our application to render the PNG images, unfortunately after performing certain operations like rotating and resizing the images (in turn these operations create and return the new BufferedImage object with updated length and width) the java heap size goes high and high to lead the OutofMemory error.

Even after closing down the current panel GC is not reclaiming the memory consumed by these BufferedImage object, I have read lot of threads mentioning that older versions of JDK itself (prior to 1.5) having memory leak in BufferedImage, but didn't find any work around or fix for this. Even in the later versions of JDK for example at the movement we are using jdk1.6.0_26 and still able to see this issue.

It will be great if someone can suggest some tips to stop the memory leak with BufferedImage object or any other alternative implementations for that object?

You should try to just use an AffineTransform with the Graphics2D method drawImage (or any other that take an AffineTransform object).

These AffineTransform objects are transformation matrices, they can keep all your image operations in one matrix and then apply that transformation matrix to the image at the cost of 1 transformation.

You can do any of four things with the transformation matrix:

  • translation
  • rotation
  • scale
  • shear

Also this way, you won't have to construct a new BufferedImage every time you apply a tranformation.

We are having the same trouble here. We are using a lot of JChart instances and the memory is leaking easily.

All leaks occur in java.awt.image.BufferedImage .

The solution that we found is:

  • Remove the Object reference BufferedImage in your case. object.remove() or object = null
  • Call Garbage Collector System.gc() . That will realy free your memory.

But GC's use is a bit expensive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM