Java内存泄漏
private boolean refreshResponseWindow(MessageObject message) {
this.responsePanel.removeAll();
this.responsePanel.add(message.buildGUI());
this.responsePanel.validate();
message = null;
return true;}
现在出现的问题是,当我收到越来越多的消息时,Java程序的内存使用量不断增加,最终导致它锁定。我已经将内存泄漏隔离到上面的代码中,特别是add过程调用。我假设removeAll会清除我面板中的内容,但它似乎仍在继续增长。
注意: message.buildGUI()返回在responsePanel上显示的JPanel
跟进:
BuildGUI代码如下所示
public JPanel buildGUI() throws Exception {
JPanel busPanel = new JPanel();
busPanel.setLayout(new GridBagLayout());
busPanel.setPreferredSize(new Dimension(Globals.panelW, Globals.panelH));
busPanel.setMinimumSize(new Dimension(Globals.panelW, Globals.panelH));
final JLabel headingLabel = new JLabel();
headingLabel.setFont(new Font("", Font.PLAIN, 18));
headingLabel.setText(this.name);
final GridBagConstraints gridBagConstraints_heading = new GridBagConstraints();
gridBagConstraints_heading.gridwidth = 2;
gridBagConstraints_heading.gridy = 0;
gridBagConstraints_heading.gridx = 0;
busPanel.add(headingLabel, gridBagConstraints_heading);
//Many more gui components marked as final
return busPanel;没有侦听器返回的面板仅用于显示。
发布于 2009-10-01 14:30:41
buildGUI方法是否向您的业务对象添加侦听器?这是内存泄漏的常见来源。调用responsePanel.removeAll()将删除这些组件,但这些组件仍可能注册为responsePanel或其他对象上的侦听器,从而使它们保留在内存中。
https://stackoverflow.com/questions/1504292
复制相似问题