C # - High memory consumption when calling method

0

I'm working with a main Form and it has several Panels. In one of them, the "general panel" I use to call instances of new forms and show on it using the following code from the FormCall class:

public void chamaFormulario(Form form)
{            
        Form activeForm = frmMain.ActiveForm;
        foreach (Panel painel in activeForm.Controls )
        {
            if(painel.Name == "panelGeral")
            {
                painel.Controls.Clear();
                if(form != null)
                {
                    form.TopLevel = false;
                    painel.Controls.Add(form);
                    form.Show();
                }
            }
        }            
}

For example, when the program opens I call a Welcome Form as follows:

private void frmMain_Shown(object sender, EventArgs e)
    {
        frmInicio frm = new frmInicio();
        formCall = new FormCall();
        formCall.chamaFormulario(frm);
    }

When I call the Form frmClients and when I open it, it has a "Back" button that aims to return to the welcome screen, which calls the following method:

private void btVoltarClientes_Click(object sender, EventArgs e)
    {
        frmInicio frm = new frmInicio();
        FormCall form = new FormCall();            
        form.chamaFormulario(frm);
    }

But looking at the memory consumption in the VS2017 Community diagnostic tool, which was consuming 60MB on to 96MB, and if I re-open the client form and press to go back again, it goes up to 136MB ... And so successively.

What should I do to resolve this high memory consumption?

    
asked by anonymous 28.05.2018 / 20:49

1 answer

2

Try removing controls from the dashboard (% w / o%) by discarding the forms:

painel.ControlRemoved += (ss, ee) => { ee.Control.Dispose(); };
    
28.05.2018 / 21:00