1

I have an out of memory exception when working with javax.Swing and here's what I did. I loaded 16 jpgs (70*70 pixels each) and had 16 Jbuttons. I set the ImageIcon of the JButtons to different jpgs very quickly in a loop, basically as quickly as my CPU can handle . The memory usage is increased radically. The problem is, I never reloaded image. There are only 16 images and all the assignments are passed by reference right? I run Eclipse Memory Analyzer and here's what I got:

One instance of "java.awt.image.FilteredImageSource" loaded by " &lt system class loader &gt " occupies 703,066,368 (43.58%) bytes. The instance is referenced by sun.awt.image.ToolkitImage @ 0x7859cef60 , loaded by " &lt system class loader &gt ". The memory is accumulated in one instance of "java.util.Hashtable$Entry[]" loaded by "&lt system class loader &gt".

Keywords java.awt.image.FilteredImageSource
java.util.Hashtable$Entry[]

Can I suspect there's a memory leak in Java's swing implementation? Any ideas?

Basically the code looks like:

    public class Test{
        private static ImageIcon[] imgIcon;
        private static JButton[] buttons;
        private static JFrame myFrame;
        public static void main(String[] args){
            myFrame = new JFrame(new GridLayout());
            for(int i = 0; i &lt 16; i ++){
                 imgIcon[i] = //load the imageIcons
                 buttons[i] = new JButton();
                 myFrame.add(buttons[i]);
            }
            myFrame.pack();
            myFrame.setVisible(true);
            new Thread(new Runnable(){
                @Override
                public void run(){
                    while(true){
                         for(int i = 0; i &lt 16; i ++){
                               SwingUtilities.invokeAndWait(new Runnable(){
                                       public void run(){
                                            buttons[i].setIcon(imgIcon[/*random num 1-16*/]);
                                       });
                         }
                     }
                    }
            }).start();
         }
    }

Please disregard the apparent anti-pattern here because I abstracted away a lot of things going on in the new thread.

DeadChex
  • 4,379
  • 1
  • 27
  • 34
fleetC0m
  • 241
  • 1
  • 3
  • 13
  • could you post some relevant code where you load your images? It would be great if you can make a [SSCCE](http://www.sscce.org) – nachokk Sep 25 '13 at 02:06
  • @nachokk It is very difficult to abstract it into a SSCCE. I will try. – fleetC0m Sep 25 '13 at 02:08
  • Have you tried with a different set of images? – ibtarek Sep 25 '13 at 02:17
  • @ibtarek Not yet, why could possibly the image be the problem? – fleetC0m Sep 25 '13 at 02:21
  • @nachokk whoops, fixed – fleetC0m Sep 25 '13 at 02:24
  • @fleetC0m it's weird that among 16 images of the same size, there's one that needs so much memory. Does this happens always with the same image? – ibtarek Sep 25 '13 at 02:32
  • Maybe after using the image in the array, discard the image? (maybe set its spot in the array to null) I know in the past when I've attempted to load up images like that and didn't do any sort of clean up, it always was a memory hog. – DeadChex Sep 25 '13 at 02:39
  • As an alternative to loading, consider synthetic digit images, seen [here](http://stackoverflow.com/a/4151403/230513). – trashgod Sep 25 '13 at 10:27
  • 1
    It looks like `JButton.setIcon()` is a really bad way to display an image, especially if you need to change your image a lot in high frequent because somehow in the function implementation, multiple copies of the images are created and they never released. I'm now using a canvas to achieve my goal – fleetC0m Sep 25 '13 at 18:36

0 Answers0