Click here to Skip to main content
15,885,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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
Posted
Updated 1-Apr-15 23:25pm
v3
Comments
Darren_vms 2-Apr-15 4:39am    
what are you expecting to happen when the image is made larger ? why do you feel this is a problem ?
hansoctantan 2-Apr-15 5:22am    
updated my question. hope it helps
Richard MacCutchan 2-Apr-15 5:31am    
Every time you zoom you are increasing the amount of memory required to hold the image. How do you expect to do this without increasing it?
hansoctantan 2-Apr-15 5:32am    
is there a way to release this used memory?
Richard MacCutchan 2-Apr-15 6:20am    
Yes, release the image resources that are using it.

1 solution

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 ?
 
Share this answer
 
Comments
hansoctantan 2-Apr-15 8:32am    
how can i release the resizedImage when its the return of the method.
This is my 1st time manipulating image in java and 1st time to concern about the memory

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900