1

我正在将一个 Texture2D 绘制到屏幕上。此纹理以空白(完全透明)800*350 纹理开始。此纹理从 LoadContent 加载到两个不同的纹理,即空白纹理和填充纹理。然后我在更新方法中将像素绘制到填充纹理。用精灵批处理绘制填充的纹理,并重复该过程。

fillTexture 在每次更新调用开始时“更新”,以消除那里的像素,因此可以重新填充。我发现更新纹理根本没有正确摆脱旧数据,问题就从这里开始了。首先,我尝试在每次调用开始时将filledTexture = 设置为blankTexture,但这仍然保留了上一次调用的数据。然后我尝试了以下方法:

    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);
   }

现在,这些方法都可以正常工作。除了每 20 秒左右,帧率会下降大约 20 到 30 点,然后恢复正常。我看不出这是什么原因,因为所有代码路径都在不断地运行,像素数以恒定的速率上升并保持在那里,所以帧丢失不会那么不一致。

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!

4

2 回答 2

1

Just a suggestion.

The Texture2D (MSDN) class implements IDisposable, so you could run the Dispose method on the texture when you don't need it anymore. That will free any unmanaged resources used by the texture and will probably free the memory.

于 2013-02-27T21:35:39.170 回答
0

Try adding filledMap.Dispose() at the end of the Update method.

于 2013-02-27T21:41:17.803 回答