A couple of weeks ago I mentioned that I had managed to work out how to run the CLR Profiler with XNA 4. I now understand enough to work out what the results mean. At least enough to track back to the parts of my code that are allocating memory during the game and therefore creating garbage to be cleaned up!
I have not finished getting rid of all the allocations yet but I was able to run through some testing on the Xbox 360 without the Garbage Collector triggering too frequently.
I found the CLR Profiler daunting at first but once you know which bits to ignore it's not so bad.
For my purposes all I need is the Allocation Graph button.
There's a tiny bit to know before you'll get anything at all and that is to make sure the 'Allocations' Profile: is ticked before unticking 'Profiling active'. The 'Calls' profile collects a lot more data and as far as I can tell does not help with finding memory allocations.
At the point in the game where you want to start logging from simply Alt-Tab out of your game and in to the CLR Profiler window to tick the 'Profiling active' box. Then Alt-Tab back to the game. If you do it the other way round and try and tick the Profile: Allocations after starting the application then nothing is logged!
You could log everything from the start of the game but I found the graphs just far too confusing to read if I tried to do that.
When you have played enough of the game to get some data, skip back to the CLR Profiler window and 'Kill Application'.
For my purposes I can now ignore everything except the 'Allocation Graph'. Open that up and skip to the RIGHT hand end. This is the biggest tip I can give. You work backwards from the right hand end towards the left hand end.
Start at the largest allocations at the top right.
Sometimes you can easily follow the lines but when they get lost in the others, either increase the scale or just click on any one of the boxes in the line and the lines change to a cross hatching either side to make them easier to follow. You can also double click a method to see the connecting methods either side.
Just work backwards from method to method until you recognise a method in your code.
So far I have found the changes necessary to avoid the garbage have been quite minor. Most of my own allocations have been caused by List<T>.AddRange(...) where <T> is one of my own classes. This is easy to fix by using a loop instead of the built in AddRange().
I have had one where it was caused by an internal XNA framework method but luckily I did not need to be doing that during the game, so I simply paused its update during game and restarted whenever the menus open.
I'm now left with a few tiny allocations that I have to go through one at a time to eliminate them. Then I can enjoy testing again on the Xbox 360 to have a garbage collection free game, I hope.
Just came across a tutorial that explains how to use the timeline to only look at allocations over a given period. This saves having to activate the profiling mid game: