2

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?

scape
  • 652
  • 12
  • 27
  • This may be far too late for your purposes, but contenttracker.codeplex.com is able to unload individual content items, and it also handles the complex issue of internal references between content items such as Model -> Texture(s). – Aranda Dec 18 '12 at 17:33

1 Answers1

9

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.

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • as I load content and then load new content to the same texture2d object, the previous content is still in cache-- correct? i'll have to read up on when content manager decides that old cached texture is no longer useful, if ever. recreating a content manager seems like a very non-trivial way to solve a trivial issue. thanks for the info, time for me to read :) – scape Oct 18 '12 at 14:21
  • Yes, you'll receive a reference to the cached instance when loading the same asset multiple times by the same ContentManager. – Justin Skiles Oct 18 '12 at 14:24
  • Thanks for the help. I thought I could find an easy way out using the unload and load content methods, sadly I still find myself in the same predicament and it appears I'll have to extend the content manager like you said. – scape Oct 18 '12 at 15:03
  • Thank you for your posts, I was about to screw up my program!! – bash.d Aug 14 '13 at 11:23
  • are you saying that I don't have to call Dispose() on objects I no longer use? – Kokodoko Mar 29 '15 at 20:17
  • @Kokodoko The short answer is: *someone* has to call `Dispose()` on (disposable) objects once they are no longer in use. There are cases, such as with objects from `ContentManager.Load`, where someone *else* will call `Dispose()` for you. – Andrew Russell Mar 30 '15 at 12:45