Thread occupying a lot of memory

1

Come on. I'm loading Buttons in a panel through the BackGroundWorker. That is, inside this BackGround I execute a query in the bank through EF and fill in the panel with the Buttons that contains information coming from this query. The more times the thread rolls, the more memory it allocates. What do I do to prevent this consumption from growing so much?

Here is the BackGroundWorker code

private void WorkCarregaVendas_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        int i = 0;
        while (true)
        {
            Application.DoEvents();
            Application.EnableVisualStyles();
            this.BeginInvoke(new MethodInvoker(() =>
            {
                CarregaVendasAtivas();

                    CarregaMesas();
                    CarregaDeliverys();



                ExecutaPesquisaNumero(txtPesquisaNumero.Text, true);

            }));
         Thread.Sleep(TimeSpan.FromSeconds(30));
        }

    }

Here I load the buttons.

private void CarregaMesas()
    {

        foreach (BotaoOperacional Control in flMesas.Controls)
        {
            Control.Dispose();
        }

        flMesas.Controls.Clear();

        //Pois se há uma pesquisa não a necessidade de carregar todos . 
        if (txtPesquisaNumero.Text.Equals(String.Empty))
            foreach (var mesaCliente in objListaVendasAtivas.Where(x => x.IdTipoVenda == (int)EnumTipoVenda.Mesa).OrderBy(x => x.NumeroMesa))
            {
                decimal preco = 0;
                using (var insRepositorioMesaProduto = new MesaProdutoRepositorio())
                {
                    preco = insRepositorioMesaProduto.
                        PesquisaProdutoMesa(mesaCliente.Id).Select(x => x.Produto)
                        .Sum(x => x.PrecoVenda);
                }
                var btn = new BotaoOperacional(mesaCliente)
                {
                    AtualizarMenuPrincipal = CarregaMesas,
                    Aparencia = EnumTipoVenda.Mesa,
                    NumeroMesa = mesaCliente.NumeroMesa,
                    Cliente = mesaCliente.Cliente == null ? "" : mesaCliente.Cliente.Descricao,
                    Preco = preco
                };



                btn.CarregaBotao();

                flMesas.Controls.Add(btn);


            }

    }
    
asked by anonymous 08.04.2016 / 08:23

1 answer

4

Overall

To find out what is causing the unwanted increase in memory consumption, the best thing to do is to run a profile of the application. It may be that the leak is coming from this thread, it may be coming from somewhere else. Only with a vision of "inside" to respond.

The process is basically:

  • Detect which unwanted objects are not being released;
  • Trace the creation points of these objects;
  • Trace the destruction points of these objects;
  • Trace the reference points of these objects.
  • (2) is to put breakpoints and see which calls cause the objects to be created, and (3) and (4) are later traces, see where these objects should be being destroyed (and are not being) or at which points the objects are being referenced (and should not be). Referenced objects are not destroyed.

    using to clear memory

    A classic error, to look at, is whether you are using using(){} on all objects that can (or should) be disposed of deterministically.

    You mentioned that you are using EF. At all points that the context object creates, are you using a language of this type?

    using ( var ef = new SeuContexto() )
    {
        ...
    }
    

    Normal situation, in principle

    All told, what you're observing is normal . The garbage collector does not run continuously, and even when it runs, it is not always running a version that Decreases the program's apparent memory .

    Gen 2 Collections can be run with differences of several minutes between them. That is, the type of GC that low memory only occurs from time to time.

    An increase in memory is only considered "too much" or "problem" when program memory does not stabilize ever . The memory goes up for several minutes and then falls off more or less the expected behavior.

        
    08.04.2016 / 08:49