C# FAQ: How can I force garbage collection?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How can I force garbage collection?

You can't. However, you can request garbage collection. If, when, and how it runs is up to the Common Language Runtime (CLR).

To request that the CLR deallocate unreferenced objects, call:

System.GC.Collect();

In addition, to request that finalizers be run on unreferenced objects, call:

System.GC.RunFinalizers();

An unreferenced object is one which has been created, but has no references to it; that is, all references to it are null.



Personal tools