Java gif图像占用CPU和内存

Java gif图像占用CPU和内存,java,memory,cpu-usage,animated-gif,jeditorpane,Java,Memory,Cpu Usage,Animated Gif,Jeditorpane,在我的应用程序中,我大量使用了显示在JEditorPane中的动画gif图像,这是一个聊天。 现在我意识到,在某些情况下,GIF会消耗大量CPU(接近100%),并且使用的内存会无限期地增加 如何避免这种情况?或者,您可以建议另一个组件来替代JEditorPane以获得更好的性能吗 这是一个可以显示内存增加和CPU使用情况的示例 public class TestCPU extends JFrame { /** * */ private stati

在我的应用程序中,我大量使用了显示在JEditorPane中的动画gif图像,这是一个聊天。 现在我意识到,在某些情况下,GIF会消耗大量CPU(接近100%),并且使用的内存会无限期地增加

如何避免这种情况?或者,您可以建议另一个组件来替代JEditorPane以获得更好的性能吗

这是一个可以显示内存增加和CPU使用情况的示例

    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);
    }
}

测试中使用的图像

那么为什么要加载500个图像?这可能是原因,尤其是在没有实现缓存的情况下。您丢失了一个标记。500个图像只是为了重现问题,并看到CPU负载和内存增加。结束标记不改变情况…好的,如果你正在修改editorPane文本,我想所有的都被重新加载了。你试过一次添加一个JEditorPane吗?对不起,我不明白我的英语不好。。。你能提供一个例子吗?你能发布gif动画的代码吗?