Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:05:48+00:00 2026-06-13T04:05:48+00:00

I started a project with the concept of reusing the same texture2d objects continuously

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:05:50+00:00Added an answer on June 13, 2026 at 4:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started a little side project to try to learn a few new concepts
I need to solve this problem. I started my project from the wrong concept
I've started working on quite big project. Concept: Some1 inserts data to mysql. Other
I'm new to Grails and web development. I started doing a project on Schedule
I started playing around with web2py the other day for a new project. I
I have a recently started project where i'm using mongoid for the database and
I currently have a recently started project in development with a 2010 target date
I have the following text: started: Project: ProjectA, Configuration: Release Any CPU ------ I
I started my project in Asp.net MVC(c#) & SQL Server 2005.I want to implement
I recently started a project in Symfony2 and I need to run some methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.