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 pointed me in the following direction.
1 2 3 4 5 6 7 8 9
How can we easily trigger this routine in an XAF web application? First, add the following to the Global.asax.cs file:
1 2 3 4 5 6 7 8
Then whenever you want to trigger a garbage collection, visit the following URL:
You should see the text ‘Forced garbage collection OK’ in the top left of the browser window.