# Xna Xbox framedrops when GC kicks in

I'm developing an app (XNA Game) for the XBOX, wich is a pretty simple app. The startpage contains tiles with moving gif images.

Those gif images are actually all png images, wich gets loaded once by every tile, and put in an array. Then, using a defined delay, these images are played (using a counter wich increases everytime a delay passes).

This all works well, however, I noticed some small lag every x seconds in the movement of the GIF images. I then started to add some benchmarking stuff: http://gyazo.com/f5fe0da3ff81bd45c0c52d963feb91d8

As you can see, the FPS is pretty low for such a simple program (This is in debug, when running the app from the Xbox itsself, I get an avg of 62fps). 2 important settings:

Graphics.SynchronizeWithVerticalRetrace = false;
IsFixedTimeStep = false;


Changing isFixedTimeStep to true increases the lag. The settings tile has wheels wich rotate, and you can see the wheels go back a little every x seconds. The same counts for SynchronizeWVR, also increases lag.

I noticed a connection between the lag and the moment the garbage collector kicks in, everytime it kicks in, there is a lag ...

Don't mind the MAX HMU(Heap memory usage), as this is takes the amount of the start, the avg is more realistic.

Here is another screen from the performance monitor, however I don't understand much from this tool, first time I'm using it...

Hope it helps: http://gyazo.com/f70a3d400657ac61e6e9f2caaaf17587

• How long was that profiling session for? – Jonathan Dickinson Feb 21 '12 at 12:09
• About a minute or 2 – Mortana Feb 21 '12 at 13:30
• The trick is to have no garbage collection. Create a memory pool that you use anytime you allocate memory for a new object. – Nic Foster Feb 21 '12 at 15:31
• Ok - you have allocated 100MB worth of objects in 2 minutes. The XBox is very sensitive to excessive garbage. Remember anything in Update() or Draw() is getting called probably 60 times per second. – Jonathan Dickinson Feb 21 '12 at 16:13
• @JonathanDickinson That's what I noticed, my Draw() method checks the current page state, and it won't do anything unless it has the right state to act. BUT the Update() method isn't keeping the pagestate in mind, which makes it so that EVERY custom component (and there are loads of them, I made a little GUI framework) that is created, get's his Update() called. I'm pretty sure I'm doing the State stuff wrong, so I'm now into reading this: create.msdn.com/en-US/education/catalog/sample/… I'll rebuild the project tomorrow with the new screenmanager. Keep you posted! – Mortana Feb 21 '12 at 19:47

## 1 Answer

After a little research I found the culprit.

I have custom components that all derive from GameComponent, and who get added to the Component list of the main Game class.

This was one (of a total of 2) major problem, causing to update everything that wasn't needing an update. (The draw method was the only one who kept the page state in mind, and only drew if needed). I fixed this by using different "screens" (or pages as I called them), wich are the only components who derive from GameComponent.

Then I only update the page wich is active, and the custom components on that page also get updated. Problem fixed.

The second big problem, is the following; I made a class which helps me on positioning stuff on the screen, relative that is, with percentages and stuff like that. Parent containers, aligns & v-aligns etc etc. That class had properties, for size & vectors, but instead of saving the calculated value in a backing field, I recalculated them everytime I accessed a property. But calculating complex stuff like that uses references (to parent & child containers for example) wich made it very hard for the CLR, because it had alot of work to do.

I now rebuilt the whole positioning class to a fully functional optimized class, with different flags for recalculating when necessairy, and instead of drops of 20fps, I now get an average of 170+fps!