tags:

views:

104

answers:

1

Java Memory Leak

private boolean refreshResponseWindow(MessageObject message) {
  this.responsePanel.removeAll();
  this.responsePanel.add(message.buildGUI());
  this.responsePanel.validate();
  message = null;
  return true;

}

The problem that is occurring is as I receive more and more messages my Java program's memory usage continues to grow eventually causing it to lock up. I've isolated my memory leak to the code above specifically the add procedure call. I'd assume the removeAll would clear the contents of my panel but it still seems to continue to grow.

Note: The message.buildGUI() returns a JPanel that is displayed on the responsePanel

Follow up:

The BuildGUI code appears as such

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;

There are no listeners the returned panel is for display purposes only.

+4  A: 

Does the buildGUI method add listeners to your business objects? That's a common source of memory leaks. Calling responsePanel.removeAll() will remove the components, but the components may still be registered as listeners on the responsePanel or other objects, causing them to be retained in memory.

Sam Barnum