[英]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),所以我猜想該方法已經淘汰了。 有任何想法嗎?
謝謝您的幫助!
只是一個建議。
Texture2D
( MSDN )類實現了IDisposable
,因此,當不再需要紋理時,可以在紋理上運行Dispose
方法。 這將釋放紋理使用的所有非托管資源,並可能釋放內存。
嘗試在Update方法的末尾添加filledMap.Dispose()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.