-
Bug
-
Resolution: Fixed
-
P4
-
1.2.0, 1.2.1
-
1.2.2
-
generic, x86, sparc
-
generic, solaris_2.6, windows_nt
Name: krT82822 Date: 11/19/98
There appears to be major memory leaks when using
swing components. The attached program
shows a dialog. After the dialog is closed and
disposed, the dialog is never GC'd also leading
to an OutOfMemory error message.
Changing the app to use AWT components
fixes the leaks. This problem happens with
JDK1.2rc1(win32) and JDK1.2rc2(win32) and
JDK116 (Solaris) using swing103. I haven't tested
with other VM's.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class PaulOne extends JFrame {
JPanel topPanel = new JPanel();
JButton pressButton = new JButton("Press Me");
JButton exitButton = new JButton("Exit");
JLabel label1 = new JLabel();
public PaulOne() {
this.getContentPane().setLayout(new BorderLayout());
this.setSize(new Dimension(550, 65));
this.setTitle("Memory Leak Bug");
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
pressButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
pressButton_actionPerformed(e);
}
});
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.getContentPane().add(topPanel, BorderLayout.NORTH);
topPanel.add(exitButton);
topPanel.add(pressButton);
topPanel.add(label1);
label1.setText("Free Memory: " + Runtime.getRuntime().freeMemory() );
setVisible(true);
}
void pressButton_actionPerformed(ActionEvent e) {
MyDialog centrePanel = new MyDialog();
centrePanel.show();
centrePanel.dispose();
System.gc();
label1.setText("Free Memory: " + Runtime.getRuntime().freeMemory() );
}
public static void main(String[] args) {
PaulOne f = new PaulOne();
}
}
class MyDialog extends JDialog {
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 30;}
public Object getValueAt(int row, int col) {
return new Integer(row*col);
}
};
JButton closeButton = new JButton("Close Me");
public MyDialog() {
setSize(new Dimension(582, 431));
getContentPane().setLayout(new BorderLayout());
getContentPane().add( new JScrollPane( new JTable(dataModel) ),
BorderLayout.CENTER);
getContentPane().add( closeButton, BorderLayout.NORTH );
getContentPane().add( new JLabel("Free Memory: " +
Runtime.getRuntime().freeMemory() ), BorderLayout.SOUTH );
setTitle("Hello");
setModal(true);
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
}
}
(Review ID: 43012)
======================================================================
- duplicates
-
JDK-4192103 Swing fails to generate exception with remove() from JDialog, JFrame, etc.
-
- Closed
-
- relates to
-
JDK-4196661 JDialogs not being garbage collected
-
- Closed
-