Free up memory of objects

Asked

Viewed 203 times

3

How can I free up memory of objects beyond class IDisposable?

foreach (var conta in listItensContas)
{
 ItemPedidoImp itensPedidoToImp = new ItemPedidoImp()
  {
   Quantidade = conta.Quantidade,
   Descricao = conta.Descricao,
   ValorTotal = conta.ValorTotal,
   Data = conta.Pedido.DataPedido
  };
 itensImp.Add(itensPedidoToImp);  

}

In this code snippet an object is created for each item. In creating a new object, the previous one is overwritten, or a new one is allocated in memory?

  • Title referring to a doubt and in the context another doubt. Every new is a new instance so your list created objects are unique does not repeat no overwritten. The Idisposable interface is another implementation story of it is simple but there is the right time to do this should be studied and on the Microsoft website explains how it should be done.

  • Site https://docs.microsoft.com/pt-br/dotnet/api/system.idisposable?view=netframework-4.8 easy to understand

1 answer

3


There is no class IDisposable. There is an interface that serves to adopt a design pattern called Disposable or Disposable. You need to implement this standard properly, which is not easy, in your class for it to dispose of unmanaged resources. If you do not make manual release within this standard no release will occur, this standard only automates the release call that was made. If you don’t know how to do this don’t create a class that needs it, so avoid abstractions that require it. It’s hard and few people know how to do it right.

Note that I spoke about unmanaged resources. Managed memory doesn’t have to do anything, the Garbage Collector will do everything for you if you do not have some crazy code that prevents this, but it is even difficult to create a code that generates a problem like this.

If you are having memory problems the problem can be quite different from where it seems, it appears when everything is already clogged and not where the clogging begins.

Every time he gives one new is creating a new object (an allocation is made), so it is not passing over anything. And in the list also you are not putting on top because always adding a new element and not using an existing one. If you want some to be discarded, and I don’t see why you would want that in this case, but it might just be because I don’t know the whole context, then you would have to make a logic that reuses the object instead of creating a new one.

  • I am one of those who does not know how to implement this standard, when I need to free up memory, I make the class inherit the interface Idisposable, implement a method public void Dispose()
 {
 GC.SuppressFinalize(this);
 } I was taught to do so , but I don’t know how it works. Where can I find more information on how to implement correctly?

  • 3

    This is not liberating anything and may even be avoiding some liberation. If taught to do so, taught wrong. There’s more material in the documentation, but you have to pay close attention. In general people do not err because they taught wrong, is that they do not pay attention in all details and start to do things different than what was taught. Right here if you search on is full of question on the subject. That same case I doubt it makes sense to do this, but I can only say seeing everything.

  • Very interesting, I will read and reread the documentation until I can understand, because where I work is that they "free memory", in the end did not solve anything, it was of great help this information the performance of the system will improve a lot with this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.