Is there a way to force C#/Mono to dispose of a variable?

I am having trouble with both memory management and garbage collection, which could I believe all easily be solved if I could simply delete a particular object from memory when I choose to, rather than waiting for the GC to come around and do it for me. All of the “help” I can find on Google amounts to “don’t worry, C# does it for you”… but we all know how well that works in games, and I can’t find anything about how to do it manually. Any suggestions?

There’s not really a ‘delete’ command in C#/mono/.net.

Unmanaged types (like IO.Stream, or things of that nature) usually implement IDisposable so that they can be cleared out from memory quickly and easily, but it’s really only special types that do this.

Clearing out say an ‘array’ is a ‘wait for GC’ type thing.

I’ll often times force a GC.Collect() if I’ve just done something super massive and want to clean up as best as possible immediately while I know I still can (like say during a load scene process I build out a lot of stuff creating loads of arrays and enumerables along the way).

This is one of the downsides to managed memory. One of several…

Usually you can do:

theReallyBigObject = null;
GC.Collect();

but this is assuming that “theReallyBigObject” is the only reference to that object. If there are active references to an object elsewhere, there’s no way to delete it. If you’re going to use GC.Collect(), it’s best to put it somewhere where it’s ok to have a hitch in framerate, like call it when the player is opening or closing a menu, or during a “Loading…” screen instead of while they’re in the middle of running around shooting things.