简体   繁体   English

BufferedImage对象占用大量内存

[英]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. 不幸的是,我们在应用程序中一直使用BufferedImage对象来渲染PNG图像,不幸的是,在执行了某些操作(如旋转和调整图像大小)之后(这些操作又创建并返回了具有更新的长度和宽度的新BufferedImage对象),Java堆大小变大了和高电平会导致OutofMemory错误。

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. 即使关闭了当前的面板GC,也没有收回这些BufferedImage对象消耗的内存,但我读了很多线程,提到JDK本身的早期版本(1.5之前的版本)在BufferedImage中存在内存泄漏,但是没有找到任何工作解决或解决此问题。 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. 即使在移动中的JDK更高版本中,例如,我们也使用jdk1.6.0_26,并且仍然能够看到此问题。

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? 如果有人可以建议一些技巧来阻止BufferedImage对象或该对象的任何其他替代实现的内存泄漏,那就太好了吗?

You should try to just use an AffineTransform with the Graphics2D method drawImage (or any other that take an AffineTransform object). 您应该尝试仅将AffineTransformGraphics2D方法drawImage一起使用 (或使用AffineTransform对象的任何其他方法)。

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. 这些AffineTransform对象是变换矩阵,它们可以将所有图像操作保持在一个矩阵中,然后以1个变换的代价将该变换矩阵应用于图像。

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. 同样,通过这种方式,您不必每次应用转换都构造一个新的BufferedImage。

We are having the same trouble here. 我们在这里遇到同样的麻烦。 We are using a lot of JChart instances and the memory is leaking easily. 我们正在使用许多JChart实例,并且内存很容易泄漏。

All leaks occur in java.awt.image.BufferedImage . 所有泄漏都发生在java.awt.image.BufferedImage

The solution that we found is: 我们找到的解决方案是:

  • Remove the Object reference BufferedImage in your case. 在您的情况下,删除对象引用BufferedImage object.remove() or object = null object.remove()object = null
  • Call Garbage Collector System.gc() . 调用垃圾收集器System.gc() That will realy free your memory. 那将释放您的记忆。

But GC's use is a bit expensive. 但是GC的使用有点昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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