Java gif image consuming CPU and memory
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.
*/
publi开发者_如何学Goc 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
精彩评论