开发者

Reading image using Javax takes a lot of memory

开发者 https://www.devze.com 2023-03-06 00:30 出处:网络
I\'m opening bunch of files using JFileChooser and for each image I create BufferedImage using image = ImageIO.read(path);. Where image is declared as a static field.

I'm opening bunch of files using JFileChooser and for each image I create BufferedImage using image = ImageIO.read(path);. Where image is declared as a static field.

Now I've got 30files 1Mb each, and after running 60times read() my memory usage (checked in OS programs manager) grows for about 70Mb.

Because my image variable is static it's not the issue that image content is stored somewhere. So my questions is, why I'm loosing so much memory?

I'm writing app that need to load tons of pictures to memory, is there somewhere a leek? Is it garbage collector task to clean unused data?

Here is my code to read this data:

public class FileListElement {

private static final long serialVersionUID = -274112974687308718L;

private static final int IMAGE_WIDTH = 1280;

// private BufferedImage thumb;
private static BufferedImage image;开发者_如何学编程
private String name;

public FileListElement(File f) throws IllegalImageException {
    // BufferedImage image = null;
    try {
        image = ImageIO.read(f);
        // if (image.getWidth() != 1280 || image.getHeight() != 720) {
        // throw new IllegalImageException();
        // }
    } catch (IOException e) {
        e.printStackTrace();
    }
    image.flush();
    //
    // thumb = scale(image, 128, 72);
    // image = null;

    name = "aa";
}
}

What's wrong with it?

Maybe I'm doing sth wrong? I need raw pixels from tons of images or compressed images loaded to RAM. So that I could fast access to any pixel of the image.

It's odd that loading 1Mb pic takes much more than 1Mb.


You can't count on the current memory usage being the amount of memory that is needed the garbage collection does not run constantly, especially if you are far from your max memory usage. Try loading more images, you might find there is no issue.

It's odd that loading 1Mb pic takes much more than 1Mb.

Well, I would expect the format stored on disk to possibly be compressed/smaller than a BufferedImage in memory. So I don't think this is odd.

0

精彩评论

暂无评论...
验证码 换一张
取 消