Wondering if I should delete a texture manually or not(?)

I was taking a texture that was loaded with the content manager and then blowing it up 2x or 3x and putting it in another texture. Should I manually dispose() of it at the end or let monogame/c# remove it? The Gemini said it would be better form to delete/dispose it.

MG/C# won’t free the memory from a loaded asset until the game is closed. If you’re done with it, you can fire Dispose to release its memory. The only reason you would not want to is if doing so causes stuttering or some such (depending on how this is used/timed within the context of your game), in which case it’s up to you to schedule it to be disposed of at a convenient time later or just eat the memory cost of leaving it until the game is closed.

The ContentManager manage the textures into a Dictionary.
You can’t dispose a specific texture from the ContentManager. It’s only all or nothing.
You have 2 solutions :

  1. keep the texture in the ContentManager as long as the game is running.
  2. Create a new ContentManager, load the texture into this new one, and when you don’t need the texture anymore, you can dispose the ContentManager.

My recommandation is to keep the texture into the ContentManager.
If you have thousand of assets to load and dispose, you have already crate a new ContentManager.

DO NOT FORGET : there’s no intelligence in AI…just probabilities.

Upon a second look, it seems like just disposing the Texture2D (when loaded from ContentManager) isn’t enough. Doing so just removes a reference from GraphicsDevice. However, it appears that you can unload a single asset from a ContentManager instance via ContentManager.UnloadAsset.

I’ve been looking into the Github repository and those 2 functions were commited since 2019… Ouch. Didn’t know they exist…

1 Like

Welp, learnings all around on this one lol :slight_smile: