I noticed several threads about garbage collection on forums.xna.com this week. Is it just me or is everyone suddenly starting to worry about performance as they approach the end of the DreamBuildPlay competition?
The crucial first step in any performance investigation, and something that many people forget, is to figure out whether you even have a problem at all.
Step 1: an early-out optimization
Is your game already fast enough? If you are running at a steady 60 frames per second, you can relax. I'm always amazed by how many people get in a panic about garbage collection even though their game is running just fine.
Step 2: measure, measure, measure!
So, your game is too slow.
Why? Is this because of garbage collection, or could the problem be somewhere else?
The XNA Framework Remote Performance Monitor for Xbox 360 (now there's a catchy name if ever I heard one!) makes it easy to find out.
There are two performance counters that are particularly interesting for understanding how long your GC is taking:
The first value tells you how often the garbage collector has run. The column on the left is the total since the program started, while to the right it shows the number of collections in the last second. The latency value tells you how long the last collection took to complete. Multiplying these two numbers together will tell you exactly what percentage of your CPU is being spent on garbage collection.
An example:
Another example:
Final example:
Step 3: twin paths to nirvana
To be continued...