In XNA, what is the best method to dispose of textures I no longer need?

微笑、不失礼 提交于 2019-12-31 03:23:25

问题


I started a project with the concept of reusing the same texture2d objects continuously throughout the game, reloading new textures periodically. Over time this proved a bad idea as I was running in to: System.OutOfMemoryException

    bool loadImages(string image)
    {
        System.Diagnostics.Debug.WriteLine("beginning loading textures " + image);

        try
        {
            //image_a = null;
            //image_o = null;

            //image_o.Dispose();
            //image_a.Dispose();

            image_o = Content.Load<Texture2D>("images/"+image);
            image_a = Content.Load<Texture2D>("images/" + image+"_a");


            return true;
        }
        catch 
        { 
            System.Diagnostics.Debug.WriteLine("cannot load textures " + image);         
            image_a.Dispose(); 
            image_o.Dispose(); 
        }

        return false; //make sure the caller loads the subsequent image
    }

I guessed that XNA was holding on to the past textures with each new content load, so I rewrote the program to instead hold a list of game objects each with its own texture2d objects. Upon going through the list I would dispose of the previous list item's texture objects and load the next list item's textures to its texture objects. The list would only be traversed once over the entire game, just periodically would I move on to the next texture set, and I would only pick out 2 textures to load so in theory there should only ever be needed 2 textures in memory at all times. I still ran in to a memory error. It's as if I can't at all rely on the dispose method to release textures from memory in a timely manner.

None of the textures I'm loading is over 125KB, I'm building this for windows phone 7

What is the best method to drop the textures I no longer need? Referring to the code: should I instead create new texture2d objects, swap them, and dispose of the originals?


回答1:


OK, first a bit of background: ContentManager caches loaded objects. There's a more detailed description here (or here). But basically each ContentManager owns everything it loads. You should not call Dispose on that content. The only way to clean it up is to unload everything in the cache with ContentManager.Unload (or Dispose).

I'm guessing that the issue you're having is that you've tried to Load a texture that you've previously called Dispose on - and are then attempting to use that disposed texture.

Although I suppose you could also run out of memory because the ContentManager is storing all those (empty, disposed) Texture2D objects, preventing the garbage collector from reclaiming them (see this answer).

So, if you want to go down this route, you need to modify the caching policy of ContentManager. How to do this is explained in this blog post. But basically you inherit from ContentManager, override Load, and call ReadAsset when you want to create a new instance of an asset from a content file.

The simplest thing to do would be to disable caching entirely, and that blog post has an example of exactly that. Then you can simply manually manage the lifetime of everything that content manager loads (i.e.: call Dispose on it yourself).

If you want something a bit more sophisticated, perhaps look at the design I propose in this answer, over on the GameDev site.



来源:https://stackoverflow.com/questions/12954227/in-xna-what-is-the-best-method-to-dispose-of-textures-i-no-longer-need

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