我的 Windows Store 应用程序中存在相当严重的内存泄漏问题。由于它甚至在后台泄漏,我认为 BackgroundTask 会导致泄漏。泄漏非常严重,在 3 小时左右的时间里积累了 150MB。有任何想法吗?
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 似乎没有释放上一个,所以我有大量的列表悬而未决。