2
Answers

Cleaning Up

Marc

Marc

10y
583
1
I was wondering if there is a simple syntax similar to
 
x.dispose();
 
that I can use to make sure my objects are dumped for memeory.  I am just looking for alternate methods aside for turning my dispose method into a bool statement. Thanks in advance!
Answers (2)
1
Vulpes

Vulpes

NA 163.6k 1.5m 10y
Well, the Dispose() method only cleans up unmanaged resources such as file handles, database connections etc. It doesn't remove the managed object (x) itself from memory.

The garbage collector (GC) does that when there are no longer any references to the object at a time of its choosing.

Generally speaking, the GC does a good job and you should not try to interfere with its operation. Apart from calling Dispose() if the object implements IDisposable, the only thing you should do is to set object references to null if they are no longer needed and they aren't about to go out of scope anyway as this might enable the GC to clean up the object more quickly.

However, you can force a collection to take place with the static GC.Collect() method and, if you're about to perform a critical operation where you cannot afford the GC kicking in in the middle of it, then you can call this method first. 

Accepted
0
Marc

Marc

NA 230 35.1k 10y
Thanks for the thorough explanation!