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

  • 4
    \$\begingroup\$ Are you using C#? To me this looks like the garbage collector kicking in every once in a while. \$\endgroup\$
    – thomasD
    Apr 10, 2013 at 11:33
  • \$\begingroup\$ Yes I am using c # \$\endgroup\$ Apr 10, 2013 at 12:15

1 Answer 1


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.

  • \$\begingroup\$ 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? \$\endgroup\$ Apr 10, 2013 at 12:27
  • \$\begingroup\$ @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 :-) \$\endgroup\$
    – Marton
    Apr 10, 2013 at 12:32
  • \$\begingroup\$ 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 \$\endgroup\$ Apr 10, 2013 at 12:59
  • \$\begingroup\$ @MehdiBugnard You're welcome! \$\endgroup\$
    – Marton
    Apr 10, 2013 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .