Unloading textures

MonogGame 3.6, Windows DirectX

How do you unload (free up memory) and later reload the same texture?

Doing the following result in a disposed texture that isn’t usable (nothing renders for an opaque texture).

            var t = Content.Load<Texture2D>(name);
            Console.WriteLine(t.IsDisposed);    // False, usable content
            Console.WriteLine(t.IsDisposed);    // True, unusable content
            t = Content.Load<Texture2D>(name);
            Console.WriteLine(t.IsDisposed);    // True, unusable content

Is this pattern suppose to work?
If not, how are you supposed to free up some texture memory?

1 Like

I belive the content is cached untill you unload it.

So if I create a game with 100 levels and each level have different assets… after playing through 100 levels ALL textures from ALL levels will be in the GPU memory?

Doesn’t make sense at all to me…
You really need to have a way to free CPU/GPU assets from memory in a game engine/framework IMO.

…and according to my findings the Dispose call seems to free the memory… but when I reload a previously disposed asset, it’s still marked as disposed and doesn’t function.

What I’m actually trying to do is like an image slideshow with A LOT of images (not really like this so loading actual images and copy to a texture isn’t really an option).

Thus I like to load one image while showing the another, then at the given pace switch it (and free the previous image).

The only way to do it as I can see it now is to create a Content Bundle for each image, then unload that bundle.

When creating objects with a ContentManager the Load method creates an asset in memory (calling load again for the same asset returns the same object instance). The Unload method disposes all assets loaded by the ContentManager.

Calling dispose directly on objects should only be done for objects loaded outside of a ContentManager. For example creating a texture using SetData and then disposing of that texture later.

Multiple ContentManagers can use the same mgcb file though, so there is no need to break up the Content project unless you want to.

For example I usually have one mgcb file but multiple ContentManagers for main resources, levels and temporary editor data. The main resources are never unloaded, levels are unloaded during the start of the level load screen and temporary editor data is unloaded per asset processed. However you organize this though is entirely up to you.

1 Like

I was a bit confused about this before.
I realized that I could use the same textures and do Content.Load on them and it would automatically replace the data properly. (same thing goes for loading new levels)
For (possibly better) efficiency, I put each picture for the films onto a few big textures - so it would use a source rectangle that moved 256 pixels to the right each time and then down 256 pixels after reaching the right side of the texture (4096) [and reset x back to zero]. Although maybe this is even necessary, since the delay between frames is longer than the time to load(over-write) each picture.
So basically I realize that a Content.Unload isn’t needed unless it’s a specific Content that you’ll never re-use for anything (like a very specific style of cut-scene - in which case freeing it’s a good idea).