繁体   English   中英

如何释放Texture2D数据/内存? XNA

[英]How to free up Texture2D data/memory? XNA

我在屏幕上绘制了一个Texture2D。 该纹理从空白(完全透明)800 * 350纹理开始。 该纹理从LoadContent加载到两个不同的Textures中,分别为blankTexture和filledTexture。 然后,在我的更新方法中将像素绘制到filledTexture。 用sprite批处理绘制filledTexture,然后重复该过程。

在每次更新调用开始时,都会对“ filledTexture”进行“更新”,以消除那里的像素,因此可以对其进行重新填充。 我发现,更新纹理并不能完全消除旧数据,于是开始了问题。 首先,我尝试在每次调用开始时将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点,然后恢复正常。 我看不出有什么原因,因为所有代码路径都在不断运行,像素数以恒定的速率上升并保持在那里,因此帧丢失不会如此不一致。

我打开任务管理器,看了看物理内存,aaa,结果发现RAM每20秒左右就装满并转储一次。 在注释掉在每次更新开始时将filledMap设置为新的Texture2D的代码行之后,内存泄漏消失了(每20秒填充和转储大约1.5 gigs)。 因此,我需要一些有关如何正确清除内存的想法! 使用texture.SetData的帧速率成本太高(它将很快下降到约2 FPS),所以我猜想该方法已经淘汰了。 有任何想法吗?

谢谢您的帮助!

只是一个建议。

Texture2DMSDN )类实现了IDisposable ,因此,当不再需要纹理时,可以在纹理上运行Dispose方法。 这将释放纹理使用的所有非托管资源,并可能释放内存。

尝试在Update方法的末尾添加filledMap.Dispose()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM