Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I developed a small xna games. For some time I have a problem with the saturation of RAM. In fact everything works perfectly, the game runs with a "Frame" of 60fps. But strangely I do not know for what reason my games every 4 seconds freeze half a second.

Yet my average call time of the "Update" function is approximately 7 ms (which is supposed to be good ..)

Here is a screen capture of the Task Manager. I can see the ram flush periodically.

Have you any idea why the ram saturates at only 50%?

enter image description here

share|improve this question
Are you using C#? To me this looks like the garbage collector kicking in every once in a while. –  thomasD Apr 10 '13 at 11:33
Yes I am using c # –  Mehdi Bugnard Apr 10 '13 at 12:15
add comment

1 Answer

up vote 7 down vote accepted

This is probably due to the Garbage Collection kicking in every x seconds. In general, you should minimize (or even eliminate) creating "garbage" in an XNA game. Examples of things that could create garbage during the game loop:

  • Calling anonymous methods or closures.
  • Setting/clearing delegates or event handlers.
  • Using an enum as a key in a dictionary. Simply doing a lookup creates garbage in this case.
  • Pretty much any string manipulation. For example, doing "silly" things like wanting to create strings using "+" or "string.Format, etc.
  • Adding/removing items out of a LinkedList. It is very common to have objects added/removed from linked lists in the game loop. So now you have to roll your own linked list, or use something other than linked lists.
  • Accessing value types via an interface will cause them to be boxed (Is this really true? I was not aware of this).
  • Some enumerators create garbage. Good luck figuring out which ones do and which ones don't. Quick: Does foreach() on a Dictionary ValuePair create garbage? Often it seems safer just to fall back to for() if you are unsure (where the collection allows this).
  • Using yield in order to enumerate objects.
  • Calling List.AddRange(), even if there is sufficient capacity to hold all items.

(Source: Channel9.MSDN.com)

As for best practices, here's a very nice collection of articles.

share|improve this answer
Yes. Thank you very much for enlightened about my problem. I use a lot of collections List T etc. .. But is it possible to say "GC" to wait longer before releasing resources? Or just tell him to free resources only when the application is closed? –  Mehdi Bugnard Apr 10 '13 at 12:27
@MehdiBugnard Unfortunately, delaying or eliminating garbage collection wouldn't solve any issues (it would just create more). See the memory profile you just attached. If the line doesn't ever fall down (because of the GC), it will just fill the memory. Then you'll have serious issues creating new references. The best practice is to not create any garbage in the game loop. Then the GC won't kick in. You'll need to refactor your source code. I had to go through this in my first XNA game project, and it's a real pain. But this is how you learn :-) –  Marton Apr 10 '13 at 12:32
Ok thank you very much! Unfortunate that I was not aware of this before I start my games. Now I know what I have to do. But I have a hunch that it's almost impossible not to waste the loop games ^ ^ ..In any case! A thousand thank you friend –  Mehdi Bugnard Apr 10 '13 at 12:59
@MehdiBugnard You're welcome! –  Marton Apr 10 '13 at 13:25
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.