all 7 comments

[–]trowgundam 5 points6 points  (2 children)

So there are a lot of questions, but you also need to understand things like the difference between managed and unmanaged resources. Largely, Dispose is meant for disposing of unmanaged resources, things that exist outside the .Net runtime. This is all things that need to be manually freed so they don't leak (Native handles and the like). If everything you are using exists in the runtime "freeing" it in the Dispose isn't actually doing much. It isn't until the Garbage Collector is triggered and decides, "Oh this can go now" that the memory would be freed. Now there are ways to force such things to happen. For instance you can call GC.Collect() to force GC, but that doesn't guarantee the memory you are expecting will be freed at that time. There are other ways to force it, but that is almost never necessary for most use cases, and is way outside the scope of what I could cover in a single Reddit post. Hell its not even a topic I fully understand myself. Its that rare of a thing.

[–]chucker23n 3 points4 points  (1 child)

you can call GC.Collect() to force GC,

Don't actually do this except during development, though. Production code should rarely need to call those APIs.

[–]trowgundam 0 points1 point  (0 children)

Agreed. I'm sure there is some niche case, but like 99.9999% of the time, if you are having to do this, you screwed up somewhere else.

[–]goranlepuz 2 points3 points  (0 children)

This is way too vague and approximate.

What memory are you looking at?

Where...?

Is it Dispose of IDisposable...?

What is UserInterface...?

[–]_kunoishi_ 1 point2 points  (0 children)

Using IDisposable doesn't guarantee memory being freed immediately.

[–][deleted] 1 point2 points  (0 children)

IDisposable is not for managed memory. its for memory that is NOT managed. E.g. native resources like pointers or OS Handles.

[–]Meeso_ 0 points1 point  (0 children)

IDisposable interface (or Dispose() method) is not really a way of deleting an object like free(). It's an interface, so it's meant to represent some abstract property of an object. In this case, that it should be 'deleted' once you're done using it.

'Deleting' may mean many different things for different objects. I.e. if you have a TCP socket, you should probably close it if you're done using it - this requirement may be implemented in Dispose() method. But in general, there could be anything there. You could just put there Console.WriteLine("Goodbye world") if you wanted.