问题
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 !
回答1:
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.
来源:https://stackoverflow.com/questions/9859956/xna-how-does-the-contentmanager-handle-with-memory