Managing and releasing memory

5

1) When I create an object, where it has arrays and large variables, setting them to null , will it help with the reduction? Within a method Dispose() - being that before they were with some value.

2) When I call a Dispose() , either by block using or manually, will events still fire? Will they still have these records?

3) Even after a Dispose() , does the object still exist? Because it looks like it is not deleted, then Dispose() would only be to free resources while Finalize() does not call?

  • What tips could give me to improve and optimize this issue of memory release?
asked by anonymous 02.12.2017 / 02:13

2 answers

4
  

1) When I create an object, where it has arrays and large variables, setting them to null , will it help with the reduction? Within a method Dispose() - being that before they were with some value.

Help more or less, but you should never bother to undo 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 design problem.

  

2) When I call a Dispose() , either by block using or manually, will events still fire? Will they still have these records?

What events? Call it like? What records?

Calling Dispose() manually is a mistake most of the time.

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

  

3) Even after a Dispose() , does the object still exist? because it looks like it is not deleted, then Dispose() would only be to free resources while Finalize() does not call?

Yes, the object still exists until the garbage collector claims it. Dispose() frees external resources, usually closes a file or something. Ideally Finalize() should never be called in the application. And calling manually I'd say it's always a mistake.

I've already answered a lot about this:

02.12.2017 / 14:58
1

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

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

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

link

    
02.12.2017 / 12:45