Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows应用商店应用程序背景任务内存泄漏_C#_Windows 8_Windows Runtime_Windows Store Apps - Fatal编程技术网

C# Windows应用商店应用程序背景任务内存泄漏

C# Windows应用商店应用程序背景任务内存泄漏,c#,windows-8,windows-runtime,windows-store-apps,C#,Windows 8,Windows Runtime,Windows Store Apps,我的Windows应用商店应用程序内存泄漏问题严重。因为它甚至在后台泄漏,我认为是后台任务导致了泄漏。泄漏相当严重,在3小时左右的时间内累积了150MB。有什么想法吗 public async void Run(IBackgroundTaskInstance taskInstance) { var defferal = taskInstance.GetDeferral(); var localFolder = ApplicationData.Current.RoamingFold

我的Windows应用商店应用程序内存泄漏问题严重。因为它甚至在后台泄漏,我认为是后台任务导致了泄漏。泄漏相当严重,在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);
    }
}

编辑:Nevermind,找出问题所在,它实际上在代码的另一部分。我已经创建了一个列表,每秒钟为UI分配一个元素。然后,在分配新的时,UI似乎没有发布上一个,因此我有大量的列表悬而未决。

上述代码中似乎没有任何内存泄漏。另外,对于后台任务来说,3小时150 MB的内存是相当多的。在这段时间内,它实际触发了多少次

您可以尝试不执行任何操作或禁用后台任务,以查看它是否与内存泄漏有关


不过,最好的方法是在注意到内存增加后分析内存内容。据我所知,目前没有任何第三方Windows应用商店应用程序内存分析器可用,因此您需要从中使用WinDbg。”It’这是一篇很好的博客文章,可以让你开始学习。也可能有帮助。

@Dracor UI未发布分配的列表可能与。