October 13th, 1999, 03:04 AM
Hi,
We have a problem regarding memory leaks in a Swing/AWT based
code. Any advice would be most appreciated ...
The program structure is something like this:
Pls see the handleCancel() method as below, which tries
to free the resources, but is not able to.
My further question is, if I assign some container a null
value, like in handleCacnel() below, where dlg = null,
what happens to the gui components inside the container.
I am finding that they are not disposed off.
Thanks in advance
- Ajit
(abhingarkar@ptc.com)
------------------------
// Invoked as the result of RMB click
Class CommandClass
{
Proxy p = new Proxy();
proxy.getData();
proxy.start();
}
public Class Proxy extends AbstractProxy
{
Hashtable dataStore = new Hashtable();
public Proxy()
{
super();
}
public getData()
{
//Fills in dataStore by making calls to DB
dataStore.put("Panel1Vector", someVector);
...
}
public run()
{
super.run();
Panel1 one = new Panel1();
one.createGUI(dataStore);
Panel2 two = new Panel2();
two.createGUI(dataStore);
Panel3 three = new Panel3();
three.createGUI(dataStore);
Panel4 four = new Panel4();
four.createGUI(dataStore);
tabbedPane.add("1", one);
tabbedPane.add("2", two);
tabbedPane.add("3", three);
tabbedPane.add("4", four);
}
}
public abstract Class AbstractProxy
{
protected JTabbedPane tabbedPane = new JTabbedPane();
// Create a Dialog with some buttons like Ok, Apply, Cancel, Help
PropDlg dlg = new PropDlg();
public run()
{
...
dlg.getContentPane().add(tabbedPane);
...
dlg.getOkBtn().addActionListener(this);
dlg.getApplyBtn().addActionListener(this);
dlg.getCancelBtn().addActionListener(this);
dlg.getHelpBtn().addActionListener(this);
}
....
....
***************************************
// As a result of the click on Cancel btn on Dialog, control comes here
public void handleCancel()
{
dlg.dispose();
dlg = null;
}
// AND THIS DOES NOT FREE THE RESOURCES/MEMORY
// For each invokation, I loose about 1.5 MB of memory
***************************************
}
// A typical panel class implementation
public class Panel1 extends JPanel
{
Panel1GUI gui = new Panel1GUI();
public void createPanel(Hashtable dataStore)
{
gui.createGUI(dataStore);
add(gui);
}
}
public class Panel1GUI extends JPanel
{
public void createGUI(Hashtable dataStore)
{
// Construct some gui ..
}
}
We have a problem regarding memory leaks in a Swing/AWT based
code. Any advice would be most appreciated ...
The program structure is something like this:
Pls see the handleCancel() method as below, which tries
to free the resources, but is not able to.
My further question is, if I assign some container a null
value, like in handleCacnel() below, where dlg = null,
what happens to the gui components inside the container.
I am finding that they are not disposed off.
Thanks in advance
- Ajit
(abhingarkar@ptc.com)
------------------------
// Invoked as the result of RMB click
Class CommandClass
{
Proxy p = new Proxy();
proxy.getData();
proxy.start();
}
public Class Proxy extends AbstractProxy
{
Hashtable dataStore = new Hashtable();
public Proxy()
{
super();
}
public getData()
{
//Fills in dataStore by making calls to DB
dataStore.put("Panel1Vector", someVector);
...
}
public run()
{
super.run();
Panel1 one = new Panel1();
one.createGUI(dataStore);
Panel2 two = new Panel2();
two.createGUI(dataStore);
Panel3 three = new Panel3();
three.createGUI(dataStore);
Panel4 four = new Panel4();
four.createGUI(dataStore);
tabbedPane.add("1", one);
tabbedPane.add("2", two);
tabbedPane.add("3", three);
tabbedPane.add("4", four);
}
}
public abstract Class AbstractProxy
{
protected JTabbedPane tabbedPane = new JTabbedPane();
// Create a Dialog with some buttons like Ok, Apply, Cancel, Help
PropDlg dlg = new PropDlg();
public run()
{
...
dlg.getContentPane().add(tabbedPane);
...
dlg.getOkBtn().addActionListener(this);
dlg.getApplyBtn().addActionListener(this);
dlg.getCancelBtn().addActionListener(this);
dlg.getHelpBtn().addActionListener(this);
}
....
....
***************************************
// As a result of the click on Cancel btn on Dialog, control comes here
public void handleCancel()
{
dlg.dispose();
dlg = null;
}
// AND THIS DOES NOT FREE THE RESOURCES/MEMORY
// For each invokation, I loose about 1.5 MB of memory
***************************************
}
// A typical panel class implementation
public class Panel1 extends JPanel
{
Panel1GUI gui = new Panel1GUI();
public void createPanel(Hashtable dataStore)
{
gui.createGUI(dataStore);
add(gui);
}
}
public class Panel1GUI extends JPanel
{
public void createGUI(Hashtable dataStore)
{
// Construct some gui ..
}
}