How can I force a full garbage collection easily within an ASP.NET application? The method here is for XAF web applications but the same approach should work with any ASP.NET app.
First up: Never mess with the .NET garbage collector.
I sometimes mess with the garbage collector in .NET when I’m trying to pin down some memory problem. Also, after a load test, I prefer to force the garbage collector to collect everything it can so that I can check that the memory drops as expected.
Garbage collection in .NET is complex and it is hard to be sure you’ve done it correctly. [This ancient article by Tess Ferrandez](/// See http://blogs.msdn.com/b/tess/archive/2006/02/02/net-memory-my-object-is-not-rooted-why-wasn-t-it-garbage-collected.aspx ) pointed me in the following direction.
| |
How can we easily trigger this routine in an XAF web application? First, add the following to the Global.asax.cs file:
| |
Then whenever you want to trigger a garbage collection, visit the following URL:
http://.../Login.aspx?ForceGC=1
You should see the text ‘Forced garbage collection OK’ in the top left of the browser window.