1
我的Windows Store應用程序中有一個相當有問題的內存泄漏。由於它在後臺泄漏,我認爲BackgroundTask會導致泄漏。泄漏是相當嚴重的,在3個小時左右建立了150MB。有任何想法嗎?Windows Store應用程序BackgroundTask內存泄漏
public async void Run(IBackgroundTaskInstance taskInstance)
{
var defferal = taskInstance.GetDeferral();
var localFolder = ApplicationData.Current.RoamingFolder;
var file = await localFolder.CreateFileAsync("notes.txt", CreationCollisionOption.OpenIfExists);
string s = await FileIO.ReadTextAsync(file);
RefreshTile(s);
defferal.Complete();
}
private void RefreshTile(string notes)
{
DateTimeFormatInfo info = DateTimeFormatInfo.CurrentInfo;
TileUpdateManager.CreateTileUpdaterForApplication().Clear();
XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideBlockAndText01);
var tileTextAttributes = tileXml.GetElementsByTagName("text");
tileTextAttributes.Item(4).AppendChild(tileXml.CreateTextNode(DateTime.Now.Day.ToString()));
tileTextAttributes.Item(5).AppendChild(tileXml.CreateTextNode(info.GetDayName(DateTime.Now.DayOfWeek)));
XmlDocument squareTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText01);
XmlNodeList squareTextElements = squareTileXml.GetElementsByTagName("text");
squareTextElements.Item(0).AppendChild(squareTileXml.CreateTextNode(DateTime.Now.Day.ToString()));
squareTextElements.Item(1).AppendChild(squareTileXml.CreateTextNode(info.GetDayName(DateTime.Now.DayOfWeek)));
IXmlNode subnode = tileXml.ImportNode(squareTileXml.GetElementsByTagName("binding").Item(0), true);
tileXml.GetElementsByTagName("visual").Item(0).AppendChild(subnode);
TileNotification tile = new TileNotification(tileXml);
TileUpdateManager.CreateTileUpdaterForApplication().Clear();
TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
string s = loader.GetString("NotepadText");
loader = null;
if (notes != null && notes.Trim() != "" && notes != s)
{
XmlDocument tile2Xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText04);
var tile2TextAttributes = tile2Xml.GetElementsByTagName("text");
tile2TextAttributes.Item(0).AppendChild(tile2Xml.CreateTextNode(notes ?? ""));
TileNotification tile2 = new TileNotification(tile2Xml);
TileUpdateManager.CreateTileUpdaterForApplication().Update(tile2);
}
else
{
TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(false);
}
}
編輯:沒關係,發現了什麼問題,並在代碼中的另一部分實際上是。我創建了一個列表,每秒將它的一個元素分配給UI。然後,當分配新的UI時,UI似乎沒有釋放前一個,所以我有大量的列表掛在空中。
NVM,在其他地方發現泄漏,編輯主帖 –
@Dracor UI未釋放分配的列表可能與[此錯誤]有關(http://connect.microsoft.com/VisualStudio/feedback/details/761770/metro -apps-與-C-重泄漏存儲器)。 –