我有以下代码作为游戏的一部分:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
terrainSprite.Draw(spriteBatch);
if (resourceMap.pixels.IsDisposed == false)
{
resources.Draw(spriteBatch, spriteFont);
}
spriteBatch.End();
base.Draw(gameTime);
//Disposes the texture here:
resources.pixels.Dispose();
}
//In the resources class
public void Update()
{
//gD = graphics device
pixels = new Texture2D(gD, 800, 353);
//big update method
//manipulate the pixels texture
}
当我打开任务管理器并查看资源监视器时,“myGame.exe”的内存使用量不断增加约 8 KB(我意识到这很小,但我的游戏拥有大量数据,所以我节省了每一点can 很重要,而且它建立起来相当快)。这是在所有其他代码被注释掉之后,除了这里显示的内容。然后,当我注释掉代码:“pixels = new Texture2D(gD, 800, 353);”时,内存使用量保持不变。我也尝试了 GC.Collect(),但没有骰子。
我还能做些什么来阻止它吗?(对不起,摆脱代码不是一种选择:p,更新纹理比我遇到的任何其他使纹理变为空白的方法要快得多)