垃圾收集器行为 [英] Garbage collector behaviour

查看:300
本文介绍了垃圾收集器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#(使用XNA 4和MonoGame)。我使用内容管理器的 Load()方法加载资源(纹理,音频)。当这些资产不再需要时,我如何从内存中正确地 Unload()

I use C# (with XNA 4 and MonoGame). I load assets (textures, audio) using the Load() method of the Content Manager. When these assets are no longer needed, how can I properly Unload() them from memory?

一旦对加载的资源的所有引用都被覆盖(或设置为null),资源将由Garbage Collector自动删除。这是正确的还是资产仍然锁定,因为它们是通过ContentManager加载的?

I assume that once all references to the loaded assets are overwritten (or set to null) the assets are automatically removed by the Garbage collector. Is this correct or are the assets still locked due to the fact that they were loaded through the ContentManager?

是否有必要调用'ContentManager.Unload()'释放一个特定CM的资产?

Is it necessary to call 'ContentManager.Unload()' to release the assets of a particular CM?

推荐答案

是的,如果你想让内容管理器中的资产从内存中完全卸载,有必要调用 Unload()。除了清除内容管理器的内部缓存(它保存对你加载的所有内容的引用),它将在任何实现<$ c $的资源上调用 Dispose() c> IDisposable 。

Yes, if you want the assets in a content manager to be completely unloaded from memory, it's necessary to call Unload(). In addition to clearing out the content manager's internal cache (which holds references to everything you've loaded), it will call Dispose() on any resource which implements IDisposable.

请注意, ContentManager 的资源是因为加载相同的资源两次并不会实际上加载相同的资源两次:第二次,它会简单地递送缓存的对象引用。

Note that the reason that a ContentManager maintains an internal cache of resources is because loading the same resource twice doesn't actually load the same resource twice: the second time, it will simply hand you the cached object reference.

这篇关于垃圾收集器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆