缩放图像时的内存问题 [英] Memory problem when scaling image
问题描述
设置宽度和高度大于原始图像时添加内存
示例我有1000x1000图像。将其缩放到900x900 / 800x800内存是稳定的,但是当内存不断增大时,内存会不断增加。
在图像缩放中使用它
Memory is adding when setting the width and height larger than the original image
Sample I have 1000x1000 image. Scaling it to 900x900 / 800x800 memory is steady but when larger the memory keeps increasing.
Use this in image zooming
private BufferedImage scale(BufferedImage image, double newwidth, double newheight) {
if(newwidth > 3 && newheight > 3) {
BufferedImage resizedImage = new BufferedImage((int) newwidth , (int) newheight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
if(newwidth < image.getWidth() || newheight < image.getHeight()) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST, 250);
}
g.drawImage(image, 0, 0, (int) newwidth , (int) newheight , null);
g.dispose();
System.gc();
return resizedImage;
} else {
return (BufferedImage) image;
}
}
这就是发生的事情。
1st Zoom - 1100x1100 - >内存200MB
第二放大 - 1200x1200 - >内存250MB
第3缩放 - 1200x1200 - >内存300MB
等等,直到内存达到1 gig
即使我重新打开另一个图像内存仍然是相同的
This is what happened.
1st Zoom - 1100x1100 -> memory 200MB
2nd Zoom - 1200x1200 -> memory 250MB
3rd Zoom - 1200x1200 -> memory 300MB
and so on till memory is up to 1 gig
even if I reopen another image memory is still the same
推荐答案
为了向前移动,最好知道
你允许什么堆大小?
您尝试过不同的GC吗? (示例-XX:+ UseG1GC)
您是否尝试过设置Min和MaxFreeRatio?
还可以使用GC设置和min / maxfreeRatio 。
正如理查德指出你需要确认图像资源是否已经发布,我看到图像被清除但是resizedImage呢?
To move this forward it would be better to know
What heap size are you allowing ?
Have you tried different GC ? (example -XX:+UseG1GC)
Have you tried setting Min and MaxFreeRatio ?
Also play with the GC settings and min/maxfreeRatio.
As Richard pointed out you need to confirm that the image resources are released, I see image is cleared but what about resizedImage ?
这篇关于缩放图像时的内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!