Take the 2-minute tour ×

I have a strange problem with XNA 4. I'm constructing a SpriteBatch using this.spriteBatch = new SpriteBatch( GraphicsDevice );. This leads to a memory leak up to 600 MB memory consumption.

This problem didn't exist two days ago.

I have a static class called Resources:

public static GraphicsDeviceManager gdm;

This gets set in the Game-constructor

public class Game : Microsoft.Xna.Framework.Game {
    public Game() {
        Resources.gdm = new GraphicsDeviceManager( this );
        Content.RootDirectory = "Content";

In the Initialize-Method I'm creating a SpriteBatch in the Resources

protected override void Initialize() {
    Resources.Game = this;
    Resources.spriteBatch = new SpriteBatch( GraphicsDevice );

At the point Resources.spriteBatch = new SpriteBatch( GraphicsDevice ); it allocates up to 600 MB memory.

Why is XNA allocating so much after I created a SpriteBatch? How can I fix it? (It worked two days ago and I didn't change anything that could cause this problem.)

share|improve this question
Nothing in the code you provided causes the memory leak. You need to post more code. Also, I doubt that anything like creating a new SpriteBatch object (except maybe if you are doing it every frame) would cause such a massive memory leak. –  Jaakko Lipsanen May 15 '13 at 19:09
add comment

1 Answer

It's not the fault of SpriteBatch. It's much more likely to be the fault of your Resources object. By the way, keeping track of resources is the job of the Game class. I would be skeptical that you have good reason to also assign a GraphicsDevice into your custom object. Afterall, Game already has one, and it ought to be a singleton.

Do you call Game.Initialize() in your Resources object? I once had similar symptoms because I called base.Initialize(); in my game's LoadContent method. (Infinite allocations until stack overflow.) You might try stepping into your code from Main by simply hitting F11. See if the execution does anything that it shouldn't be doing, and see if it is doing it over and over and over.

share|improve this answer
add comment

Your Answer


