BitFlipper said:
Minh said:
*snip*

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.

Can you be a bit specific? Which one is the garbage? float fps? or + fps.ToString("F2")?

 

fps is typically from a global variable anyway.

and fps.ToString("F2") is released right after the line ends.

if your float fps is in a method, it is released right away also.

 

And none of them has anything to do with garbage. Isn't garbage only exists when you have class object? like car carObj = new car();

Anyway, you can manually release obj too, just like C++. I am not expert on this, but, I remember they just say you shouldn't do it too often.