jersoft
jersoft

Reputation: 478

Memory not freeing after Obj.Close()

I'm having an issue with regards on memory management with our system.

Basically here is my design:

  1. Load the List of the Records. frmBrowse Memory used 90MB

  2. 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");
        }
    }
    
  3. When the user CLOSE, i actually Hide it (frmAP )to prevent re-query datas such as lookup tables and others - frmAP - Memory used 110MB

On the frmAP_Close()

this.Hide();

-Memory at 110MB

  1. After hiding the form (frmSomeForm ) the memory remains at 110MB
  2. 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

Answers (1)

thecoop
thecoop

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

Related Questions