My first post here so hi everyone!
I have a rather difficult question for you guys:
I have a relatively large binary (black & white) image on disk. Dimensions 8000x4000 px.
But because its binary, my math says its not that big in memory (8000x4000bits = around 3.81 MB).
When i load it into BufferedImage, memory consumption rises for expected amount.
But when i resize it, regardless of the ratio (be it 1 pixel resize), memory usage jumps for 250 MB!
So what’s going on and how can i make it stop?
Here is a little example i prepared to illustrate above situation:
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class BufferedImageTest {
public static void main(String[] args) {
try {
// initial: 7.8 MB
BufferedImage image = ImageIO.read(new File("images/world_test.png")); // image is 8000x4000 binary (black and white only)
// 14.4 MB
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
// 18.9 MB
Graphics2D g = result.createGraphics();
// 21.5 MB
g.drawImage(image, 0, 0, 8000, 4000, null);
// 21.5 MB
g.drawImage(image, 0, 0, 7999, 4000, null);
// 277.7 MB
} catch (IOException ex) {
Logger.getLogger(BufferedImageTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}