XNA How does the ContentManager handle with memory

后端 未结 1 1503
迷失自我
迷失自我 2020-12-06 23:26

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

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

what does h

相关标签:
1条回答
  • 2020-12-06 23:35

    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.

    0 讨论(0)
提交回复
热议问题