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?