Dispose() - Resource Release or Memory Release?

Asked

Viewed 81 times

4

After seeing the topic "I should always use Dispose?", I still had some doubts and would like to know more about.

It’s no secret that it’s ideal to use the Using statement for objects Idisposable, as described below by the Microsoft documentation itself.

The instruction using ensures that Dispose is called, even if an exception occurs within the block using. You can get the same result by placing the object inside a block Try and then calling Dispose in a block Finally.

Doubt 1: Knowing this then, there is some difference in performance in compilation time since everything will be transformed into Try/catch? What would be the ideal?

Doubt 2 (Primordial): What would be the best practice for both memory and resource release? Should I set the variable = null even being inside the block using to guarantee the 2?

  • What do you mean by the following excerpt: "But in practice we know it can be a little different, once in performance another to avoid Code Smells."?

  • I may have expressed myself badly about the text.

  • I would like to point out that I was reading the Dispose Pattern code before it was removed in the face of rain of negativity. To the author, who does not know the name, I ask you to post a comment with the link.

  • Hardly remains to say anything more that has not already been said in all these questions: https://answall.com/search?tab=votes&q=dispose.

1 answer

8


First of all, Dispose does not make "memory release". The method serves to finalize resources used.

For example: a file that has been opened for editing or a network connection.

Briefly, when a program reads/writes to disk, makes a connection to the network, among other operations, it is necessary to use operating system resources making system calls. Therefore, when finished using these resources, they need to be closed/finished and this is what the method Dispose does - or at least should do, because it will always depend on the implementation.

Memory release is something that has nothing to do with it, when you create a variable, you are allocating a space in memory and when this variable no longer needs to be used, this space can be released and give way to another data. In normal cases, you don’t have to worry about doing memory management because in an application. NET Garbage Collector does this automatically.

Knowing this then, there is some difference in performance in compilation time since everything will be transformed into Try/catch? What would be ideal?

You may have, I don’t think you can even measure effectively. But it’s certainly not something you should be worried about.

If there is a difference it must be something very small. And let’s face it, compromise the readability (or even the guarantee of the method call Dispose()) because of milliseconds (or micro?) less in the compilation does not seem to me a good exchange.

What would be the best practice for the release of both memory and resource? I must set the variable = null even being inside the using block to ensure the 2?

"Better" change context to context, there is no cake recipe.

However, set the variable to null you won’t have no effect. The only thing that will happen is that you will be spelling out the end of the use of that reference. What will probably already happen in some instructions after you do this assignment (for example: end of method for local variables).

Besides being useless, it ends up facilitating the introduction of bugs if someone tries to use that variable later.

  • Could you tell me a little bit more about the release of memory and release of resource that was somewhat misunderstood from my point of view regarding the use of Dispose()?

  • @Victorlaio I can, but what you want to know, specifically?

  • A brief explanation of each and in which Dispose() directly affects ?

  • @Victorlaio Just to see if I understand: you want to know the difference between resource release and memory?

  • Yes, and if Dispose() does the memory release, or resource release, or even both in simple terms.

  • @Victorlaio The first sentence of my answer says so.

  • @Victorlaio It became clearer?

  • Yes, thank you!!

Show 3 more comments

Browser other questions tagged

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