Minh said:
BitFlipper said:

You could mitigate the GC by pre-allocating all your objects... not for the lifetime of the game... but whatever makes sense. Like a level

Yes you would typically do that, but how do you do simple things like:


float fps = 123.4f;
string frameRateText = "FPS: " + fps.ToString("F2");


...without creating garbage? You can try to use StringBuilder but you are limited (how do you format floats, for instance?), and as soon as you call StringBuilder.ToString(), you are back to square one. Also, many of the built-in classes create garbage that you might not even know about. For instance, using enums as the key in a dictionary creates lots of garbage if you access it every frame. Or assigning delegates or events, or calling anonymous methods, etc etc.


Basically, right now we have to pick through the framework and figure out what is going to create garbage and avoid using it. I would say it is almost impossible to create a complex game that doesn't create garbage during each frame. Ideally we should have an incremental GC that doesn't trigger long GC cycles so that we don't even need to worry about what creates garbage down to this fine level. If the game creates a small amount of garbage every frame, it should not matter.