Question about Content load memory management

i have a question about contentload.
if i have
Texture txt1 = Content.load(“abc.jpg”):
Texture txt2 = Content.load(“abc.jpg”):

txt1, txt2 are they both get the same reference? And will the memory stack?

I am fairly sure the following is correct, hopefully someone more experienced will confirm.

"abc.jpg" will exist only once in the content manager (it caches all assets in a dictionary).

txt1 and txt2 will both be individual textures (each their own object) and if both used will be allocated twice on the GPU. (not sure about the allocated twice part!)

if "abc.jpg" is unloaded then trying to use either txt1 or txt2 will fail.

if "abc.jpg" remains loaded and you set txt1 to null, then txt2 can still be safely used.

Trying to explain it has raised another question for someone else.


Texture txt1 = Content.load("abc.jpg");
Texture txt2 = Content.load("abc.jpg");

Different to

Texture txt1 = Content.load("abc.jpg");
Texture txt2 = txt1;

My thoughts were as above, the first is loaded twice into two texture objects.
The second one is two references to the same texture.

If there not a method to compare the two? Within MonoGame]

Like if you deleted one, you could compare to see if they were != and thus deleting one does not destroy access via the other…

I like to stick to basics :stuck_out_tongue:


False! The ContentManager caches the loaded object (Texture2D in this case), so ReferenceEquals(txt1, txt2) will be true.

True, since the unmanaged resources will be disposed (e.g. the texture data is removed from VRAM).


No, they are the same. See answer #2.

I’ll post the most important ContentManager code related to this here.
In Content.Load this happens:

if (loadedAssets.TryGetValue(key, out asset))
    if (asset is T)
        return (T)asset;
// Load the asset.
result = ReadAsset<T>(assetName, null);
loadedAssets[key] = result;
return result;

Thank you :slight_smile: @Jjagg

So each Texture variable just references the asset(object) in the dictionary.

(I think I was thinking along the lines that Content.Load returned a new Texture based on the cached asset.)

1 Like

thanks for reply :slight_smile: