XNA SpriteBatch Stalling every Second

Public Service Announcement:

If you're doing simple 2D sprite stuff in XNA, and every second or so, the GC is hitching, it's because of SpriteBatch. Every call to SpriteBatch creates a new object, which eventually needs to be GC'd. This will give you a stall every second or so, as the GC runs and tries to clean up.

Kind of sucks really. Ultimately, it means that if you want to get around this, you're going to need to write your own Sprite rendering code that allocates everything up front, and doesn't release anything until a level change, and recycles as much as possible. Not too hard - just build a quad, and write the appropriate code to render it. But it's a lot more work than just slotting it in, unfortunately.

About the author

Simon Cooke is an occasional video game developer, ex-freelance journalist, screenwriter, film-maker, musician, and software engineer in Seattle, WA.

The views posted on this blog are his and his alone, and have no relation to anything he's working on, his employer, or anything else and are not an official statement of any kind by them (and barely even one by him most of the time).

facebook comments