3

My question is not clear at title [i can not write it exactly]

e.g Texture2D picture = Content.Load<Texture2D>("myPicture");

what does happen on memory if the code above runs ? As I know Content caches the "myPicture" to the memory and return a reference to the Texture2D picture. Am I wrong ? If "myPicture" is loaded to another Texture2D object "myPicture" is not duplicated so it returns only a reference.

Is each file (or content-file) loaded over Content cached to memory (also allocated on Ram) without duplicating ? (i believe this my question with all written above should be checked)

Thanks !

icaptan
  • 1,495
  • 1
  • 16
  • 36

1 Answers1

12

Each instance of ContentManager will only load any given resource once. The second time you ask for a resource, it will return the same instance that it returned last time.

ReferenceEquals(Content.Load<Texture2D>("something"),
                Content.Load<Texture2D>("something")) == true

To do this, ContentManager maintains a list of all the content it has loaded internally. This list prevents the garbage collector from cleaning up those resources - even if you are not using them.

To unload the resources and clear that internal list, call ContentManager.Unload. This will free up the memory the loaded resources were using. Now if you ask for the same resource again - it will be re-loaded.

Of course, if you are using those resources when you call Unload, all of those shared instances that you loaded will be disposed and unusable.

Finally, don't call Dispose on anything that comes out of ContentManager.Load, as this will break all the instances that are being shared and cause problems when ContentManager tries to dispose of them in Unload later on.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • Thanks for the post ! Morely the loaded resource are hold at memory, right ? (your sentence about unload make it clear for me, but i ask it a second time, my english is not well enough to understand completely.) – icaptan Mar 26 '12 at 13:30
  • I'm afraid I don't understand your question. – Andrew Russell Mar 27 '12 at 00:13
  • Texture2D picture = Content.Load("myPicture"); after running this line, myPicture is loaded to the memory (to computer's ram) and its reference is return by Content to Texture2D instance picture. -> true or false ? Thanks ! – icaptan Mar 27 '12 at 07:26