I’m developing an XNA game for Xbox and PC, and about half-way through my development cycle I realised I had a problem with memory allocation – or more precisely, with garbage collection.
Garbage Collection (GC) occurs when the XNA framework determines that you have allocated and freed a certain amount of memory (currently 1MB), and attempts to return the freed memory to the system so it can be re-used. It’s a useful and elegant system that makes creating software easy and fun. However, when it comes to performance, especially on the Xbox, it might bite you in the ass. A single garbage collection can cause a stutter in your application, and if this is happening multiple times per second, it’s going to make your app unusable.
So I went through my game and removed all the unnecessary allocation and freeing, and replaced it with static allocation and pools of objects to re-use.
I also created this string class, which uses a static 256 char buffer. It has lots of functions for formatting text and numbers, and a function for getting a plain C# string out of it. I found it very useful… Perhaps you will too? Feel free to use it in your projects.
Remember to make it a member or a static variable, don’t re-create it every frame! And don’t create a silly number of them… Every 4 will cost you (just over) 1KB of memory. 🙂
ESP Games is the name of my indie development studio (which is… me).