大家好,
我想知道为什么下面的代码会导致记忆泄漏(或者至少我不能通过使用flush()或dispose()来释放我的资源事件。
public static BufferedImage getRGBImage(BufferedImage image) {
BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
convertedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
return convertedImage;
}
public static BufferedImage getScaledImage(BufferedImage image) {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int height = (WIDTH * imageHeight) / imageWidth;
double scaleX = (double) WIDTH / (double) imageWidth;
double scaleY = (double) height / (double) imageHeight;
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
return bilinearScaleOp.filter(image, new BufferedImage(WIDTH, height, image.getType()));
}
public static void main(String[] args) throws IOException {
File f = new File ("C:\\Temp\\Pictures\\sample.jpg");
File f2 = new File ("C:\\Temp\\Pictures\\newFile.jpg");
BufferedImage img = ImageIO.read(f);
img = getScaledImage(img);
img = getRGBImage(img);
ImageIO.write(img, "jpg", f2);
}
我使用jdk 6u31并且我发现当我使用另一个jdk时我绝对没有问题(我尝试了一些jdk 7)。 这是一个已经解决的官方错误吗?
先谢谢。