Garbage collector behaviour

亡梦爱人 提交于 2019-12-11 11:16:57

问题


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?

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?

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/19911006/garbage-collector-behaviour

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