Memory management and release

Asked

Viewed 413 times

6

1) When I create an object in which there is arrays and large variables, defining them as null, will help in the reduction? Within a method Dispose() - before they were of some value.

2) When I call one Dispose(), so much for the block using or manually, will events still fire? You will still have these records?

3) Even after one Dispose(), there is still the object? Because from the looks of it, it is not erased, so the Dispose() would only be to release resources while the Finalize() flame?

  • What tips could you give me to improve and optimize this matter of memory release?

2 answers

4


  1. When I create an object inside arrays and large variables, defining them as null, will help in the reduction? Within a method Dispose() - before they were of some value.

It helps more or less, but you should never worry about nullifying a variable. This does not guarantee release, let alone when it will occur. If you can keep doing this kind of thing you probably have some problem with design.

  1. When I call one Dispose(), so much for the block using or manually, will events still fire? You will still have these records?

What events? Call how? What records?

Call Dispose() manually is a mistake in large parts of the time.

If you do not release events manually they will leak into memory.

  1. Even after a Dispose(), there is still the object? because from the looks of it, it is not erased, so the Dispose() would only be to release resources while the Finalize() flame?

Yes, the object still exists until the Garbage Collector claim it. The Dispose() releases external resources, usually closes a file or something. Ideally the Finalize() should never be called in the application. And calling manually I would say it is always a mistake.

I’ve answered a lot about that:

2

As you said, the using block is essential, whenever I can use the same, because it closes the connections and gives automatic device in the objects at the end of the clause.

When you call Dispose() you are "warning" the Garbage Collector that that object is ready to release memory, but it is not instantaneous.

I recommend this article/ video of André Alves Lima where he speaks exactly the context of his question with practical example that helps many people understand how this flow works

http://www.andrealveslima.com.br/blog/index.php/2017/11/22/evitando-memory-leaks-no-net-com-dispose-e-blocos-using/

Browser other questions tagged

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