XNA How does the ContentManager handle with memory

≡放荡痞女 提交于 2019-11-28 01:38:17

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!