简体   繁体   中英

ImageIO.read() memory consumption

I´m currently trying to figure out where my application has a memory leak. So, I wrote a small test program since my memory leaks seem to be related to the ImageIO.read() method. My test application consists of a simple JFrame with a JButton, which starts the following Action:

    public void actionPerformed(ActionEvent e)
    {
        File folder = new File("C:\\Pictures");
        ArrayList<File> files = new ArrayList<File>(Arrays.asList(folder.listFiles()));
        try
        {
            for (File file : files)
                ImageIO.read(file);
        }
        catch (Exception a)
        {
            a.printStackTrace();
        }
    }

Although I do NOT save the return value, that is the image, of ImageIO.read, my application has a huge memory allocation (~800 MB). For test reasons, the folder C:\\Pictures contains ~23k pictures with a total size of 25GB.

Why does ImageIO.read() reserves that much memory, even after returning and NOT saving the image anywhere else?

It doesn't 'reserve that much memory'. It can't. All that seems to have happened here is that it took about 800 image loads before GC kicked in.

Why are you loading 23k images? It seems a strange thing to do. Where are you going to display them? Do you have an extra-large screen?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM