XNA ContentManager 如何处理内存 [英] XNA How does the ContentManager handle with memory
问题描述
我的问题标题不清楚[我写不出来]
My question is not clear at title [i can not write it exactly]
例如 Texture2D 图片 = Content.Load
如果上面的代码运行,内存会发生什么?据我所知,内容将myPicture"缓存到内存中并返回对 Texture2D 图片的引用.我错了吗 ?如果myPicture"被加载到另一个 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.
是否每个文件(或内容文件)加载到缓存到内存(也在 Ram 上分配)的内容上而不复制?(我认为应该检查我上面写的所有问题)
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)
谢谢!
推荐答案
ContentManager
的每个实例只会加载任何给定的资源一次.第二次请求资源时,它将返回与上次返回的相同实例.
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
为了做到这一点,ContentManager
维护了它在内部加载的所有内容的列表.此列表可防止垃圾收集器清理这些资源 - 即使您不使用它们.
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.
要卸载资源并清除内部列表,请调用 ContentManager.Unload
.这将释放加载的资源正在使用的内存.现在,如果您再次请求相同的资源 - 它将被重新加载.
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.
当然,如果您在调用 Unload
时正在使用这些资源,那么您加载的所有共享实例都将被销毁且无法使用.
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.
最后,不要对 ContentManager.Load
产生的任何内容调用 Dispose
,因为这会破坏所有正在共享的实例并在 ContentManager
稍后会尝试在 Unload
中处理它们.
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.
这篇关于XNA ContentManager 如何处理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!