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;
}
}
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