I am drawing one Texture2D to the screen. This texture starts as a blank (completely transparent) 800*350 texture. This texture is loaded from LoadContent to two different Textures as blankTexture and filledTexture. Then I draw pixels to the filledTexture in my update method. The filledTexture is drawn with sprite batch, and the process is repeated.
The filledTexture is "renewed" at the start of each update call, to get rid of the pixels that were there, so it can be refilled. I found that renewing the texture simply did not get rid of the old data properly, and here began the issues. First, I tried setting the filledTexture = to blankTexture at the beginning of every call, but this still retained the data from the previous call. Then I tried the following:
public void LoadContent(graphicsDeviceManager graphics, ContentManager content)
{
//some code
blankTexture = content.Load<Texture2D>("blank");
filledTexture = content.Load<Texture2D>("blank");
//some more code
}
public void Update()
{
filledMap = new Texture2D(graphicsDevice.GraphicsDevice, blankMap.Width, blankMap.Height);
//some code that messed around with the pixel locations
Color[] color = new Color[1];
color[0] = someNewColor;
//some code that set the new position of the pixel
//where R is a 1x1 rectangle with some location
//this sets one pixel of the filledMap at a time at R.Location
filledMap.SetData(0, R, color, 0, 1);
}
public void Draw(SpriteBatch batch)
{
batch.Draw(filledMap);
}
Now, these methods work fine and all. Except, every 20 seconds or so the frame rate would drop by about 20 to 30 points and then return to normal. I couldn't possibly see a reason for this, as all code paths were run constantly, the number of pixels was going up at a constant rate and staying there, so the frame drops wouldn't be so inconsistent.
I opened up task manager, looked at the physical memory, aaaand it turned out the RAM was filling up and dumping itself every 20 seconds or so. After commenting out the line of code that set the filledMap to a new Texture2D at the start of each update, the memory leak was gone (about 1.5 gigs were filled and dumped every 20 seconds). So I need some ideas on how to clear up the memory properly! Using texture.SetData was too frame rate costly (it would drop to about 2 FPS quickly), so I guess that method is out. Any ideas?
Thanks for the help!