Click to See Complete Forum and Search --> : Memory leak


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 ..
}
}

DVJ
November 23rd, 1999, 11:49 AM
Hi Ajit,

I have also run into memory problems opening dialogs over and over again and cancelling them.Eventually my JVM crashed giving out of memory error.It seems that the objects do not get collected unless you code them to be so they remain in your memory.
What I did was to set every single component and object I created in the dialog to null just before disposing off the dialog and I was able to run the dialog for 20 more counts.
But this also slows down the program execution because the objects are not garbage collected
I guess what u should do is set all your objects to null when disposing off the dialog
My case has been posted on getting out of memory error. I was trying to look for a better solution then this one.I guess we just have to set all objects to null to make them candidates for garbage collection to clean up the memory.
cheers
DJ