Should I dispose of all manually created resources?

Hey everyone,

I’ve got a quick question about what I should/should not be disposing of in MonoGame. I know that everything I load using the ContentManager will do its own thing, but what about other graphics resources that I create manually, such as VertexBuffer, IndexBuffer, RenderTarget2D, Texture2D, SpriteBatch … and anything else that inherits from GraphicsResource?

The simplest case is the default code generated for a new MonoGame project; there’s a SpriteBatch being created with the ‘new’ keyword, and even though it’s a GraphicsResource that implements Dispose(), it never actually gets disposed, not even in the UnloadContent() method (which claims to be there to handle any non ContentManager content).

I’m asking this question because, while most of my stuff is created and managed by the ContentManager, I do have quite a few little things floating around which I’m wondering what to do with. I also create a LOT of VertexBuffers in my program, which were the source of a large memory leak until I started manually calling Dispose() on them before derefrencing their parent objects (nodes in a large recursive quadtree structure). I want to chase down and properly handle any other similar leaks, no matter how small they might be!

Thanks very much for your help :slight_smile:

AFAIK GraphicsDevice (GD) holds a list of all the resources created (manually and via ContentManager, they’re the same to GD). When exiting the game GD frees all that content.

Should you dispose manually? It depends on the scope of your game. If you do a simple game or a game that loads very few things, my opinion is that you don’t need to manually dispose. GD will do all the work for you on exit.

If you do a big game with hundreds of maps, textures and models you may need to dispose your resources, not because leaks, but rather for the fact you’ll just run out of (GPU) memory.

That said, the leak when creating lots of VertexBuffer may be related to this: https://github.com/MonoGame/MonoGame/issues/6861

Also, when hunting GPU leaks you may find this thread interesting: GPU memory leak If you can recompile MonoGame, that patch works very well (I first intended to use it for debug, but now I have it integrated into my local copy of MonoGame to detect leaks on the spot)

Thanks for the quick and detailed reply!

Ah OK, yeah it makes sense that something takes care of it, since every XNA/MonoGame tutorial I’ve ever seen seems to have an empty UnloadContent() method and never mentions disposing.

I do create most of my resources once at the beginning of the game, but the main area where I have to create things manually is when I load my current level. The main part is a dynamic quadtree terrain which is created procedurally based on a user-chosen seed. I generate every possible level of detail during the ‘loading’ screen, which right now, for an 8 level quadtree, leaves me with 21,845 vertex buffers. When I draw the quadtree, I just pick a list of which buffers I want to draw and send them straight into the GPU (seems to be way quicker than storing a single VertexBuffer and swapping out the data).

My problem was that every time I loaded a new level, I was just dereferencing the old terrain quadtree without disposing of its 21,845 VertexBuffers. This was causing all that unmanaged memory to become isolated and leaked.

Thanks again for the tips :slight_smile: