1

in my application i do intensive use of animated gif images displayed in a JEditorPane (this is a chat). Now i realyzed that the GIFs consume a lot of CPU (near 100% in some cases), and the memory used continued to increase indefinitely.

How to avoid this? Or, can you suggest another component to replace JEditorPane for better performance?

This is an example that can show the memory increase and the CPU usage.

    public class TestCPU extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestCPU frame = new TestCPU();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestCPU() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(TestCPU.class.getResource("/images/asd.png")));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);

        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        String html = "<html>";
        for (int i = 0; i < 500; i++) {
            html = html + "<img src="+ TestCPU.class.getResource("/images/asd.gif") + ">";
        }
        editorPane.setText(html);
        scrollPane.setViewportView(editorPane);
    }
}

the image used in the test

Laphroaig
  • 619
  • 4
  • 12
  • 26
  • so why are you loading 500 images? That might be the cause, especially if no caching is implemented. And you are missing an

    tag.

    – ThomasRS Feb 28 '11 at 23:04
  • 500 images just to reproduce the problem and see the CPU load and the memory increasing. The

    end tag to not change the situation...

    – Laphroaig Feb 28 '11 at 23:18
  • Okey, if you are modifying the editorPane text, I guess all is reloaded. Did you try adding one JEditorPane at a time? – ThomasRS Feb 28 '11 at 23:31
  • sorry i don't understand (i'm not good with english)... can you provide an example of it? – Laphroaig Feb 28 '11 at 23:38
  • Could you please post the code for animating gif's ? – Ratna Dinakar Mar 01 '11 at 01:39
  • what code? this is the only code that reproduce the problem and i posted the image used too (asd.gif) – Laphroaig Mar 01 '11 at 01:47
  • Any help? I can't believe that this little application will consume the 25% CPU and start from 120mb ram increasing infinitely... Where i'm doing wrong? – Laphroaig Mar 01 '11 at 02:11
  • I had posted a question like this one, View it also please http://stackoverflow.com/questions/8295148/why-showing-gif-image-increase-memory-continuously – Shaman Virk Nov 29 '11 at 10:44

0 Answers0