Reputation: 478
I'm having an issue with regards on memory management with our system.
Basically here is my design:
Load the List of the Records. frmBrowse Memory used 90MB
After clicking record, it will pop another form which is the detailed form of the record selected frmAP Memory used 110MB
private void ViewRecord()
{
try
{
if(oAP!=null)
oAP = new frmAP();
oAP.LoadRecordDetails();
oAP.Show();
}
catch (Exception ex)
{
clsClass.oGenMethods.ErrorMessage(ex.Message, "frmBrowse", "EditSearchFields");
}
}
On the frmAP_Close()
this.Hide();
-Memory at 110MB
When the user close the Form(frmBrowse) where "frmAP" was called/instantiate
public void tsClose_Click(object sender, EventArgs e) {
if (oAP != null)
{
oAP .Dispose();
oAP .Close();
GC.Collect();
}
}
The Problem is the Memory is Still at ~110MB Whether the List(frmBrowse) Form and the Record form(frmAp) was closed.
Please advice
Thanks in Advance
UPDATE:
i Tried to delete .Hide() to isolate the problem, but still the memory keeps growing
Upvotes: 0
Views: 159
Reputation: 46098
Calling Close
does not release the memory associated with an object. That's the job of the garbage collector. Close
or Dispose
simply tells the object to release any unmanaged memory or resources it is using. The object, and everything it's referencing, still stays in memory.
To ensure oAP
is collected, set it to null:
oAP.Close();
oAP = null;
However, this is likely the wrong thing to do. The whole point of a garbage collector is to collect memory from unreferenced objects when it's needed. So let it do its job, don't force it to run, you don't need to set oAP
to null, and oAP
and everything it references will be collected when the GC needs to.
Upvotes: 4