Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Suspect Memory Leak in Swing Components
    primarykey
    data
    text
    <p>I have an out of memory exception when working with <code>javax.Swing</code> and here's what I did. I loaded 16 jpgs (70*70 pixels each) and had 16 <code>Jbuttons</code>. I set the <code>ImageIcon</code> of the <code>JButtons</code> 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:</p> <p>One instance of "java.awt.image.FilteredImageSource" loaded by " &amp;lt system class loader &amp;gt " occupies 703,066,368 (43.58%) bytes. The instance is referenced by sun.awt.image.ToolkitImage @ 0x7859cef60 , loaded by " &amp;lt system class loader &amp;gt ". The memory is accumulated in one instance of "java.util.Hashtable$Entry[]" loaded by "&amp;lt system class loader &amp;gt".</p> <p>Keywords java.awt.image.FilteredImageSource<br> java.util.Hashtable$Entry[]</p> <p>Can I suspect there's a memory leak in Java's swing implementation? Any ideas?</p> <p>Basically the code looks like:</p> <pre><code> 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 &amp;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 &amp;lt 16; i ++){ SwingUtilities.invokeAndWait(new Runnable(){ public void run(){ buttons[i].setIcon(imgIcon[/*random num 1-16*/]); }); } } } }).start(); } } </code></pre> <p>Please disregard the apparent anti-pattern here because I abstracted away a lot of things going on in the new thread.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload