У меня есть утечка памяти в моей Навигации WPF?

Я просматриваю приложение WPF, ища утечку памяти (использующий Профилировщика Памяти МУРАВЬЕВ 5.1), и я продолжаю видеть некоторые страницы и средства управления, поднимающие память, когда они не должны быть.

Таким образом, я перехожу к Объектному Графику Хранения и видеть то, что имеет в наличии их, и я продолжаю видеть это для каждой страницы:

Объектный график хранения http://img683.imageshack.us/img683/3013/ants.jpg

Вещь, мне установили KeepAlive на ложь на каждой странице, и я не думаю, что такое свойство существует на пользовательских элементах управления.

Кто-либо может сказать мне, что я должен искать? Это - даже утечка памяти или является этим нормальным поведением для приложения WPF?

7
задан Brandon 17 December 2009 в 22:31
поделиться

1 ответ

Yes, according to what you've provided, you have a memory leak. When you found the references chain, and it's not in your code, the easiest way to go would be... Reflector.

Image says: JournalEntryKeepAlive._keepAliveRoot field holds a reference to the object. Let's go in Reflector and see how this guy is hooked with our object.

This time it was easy, and all traces lead to NavigationService.MakeJournalEntry() function and then to NavigationService.IsContentKeepAlive(). Here it is:

internal bool IsContentKeepAlive()
{
    bool keepAlive = true;
    DependencyObject dependencyObject = this._bp as DependencyObject;
    if (dependencyObject != null)
    {
        keepAlive = JournalEntry.GetKeepAlive(dependencyObject);
        if (!keepAlive)
        {
            PageFunctionBase base2 = dependencyObject as PageFunctionBase;
            bool flag2 = !this.CanReloadFromUri;
            if ((base2 == null) && flag2)
            {
                keepAlive = true;
            }
        }
    }
    return keepAlive;
}

Now you know the rules. Object is kept in memory if:

  • It's not a dependency object;
  • Attached propery JournalEntry.KeepAlive is true;
  • It's not a PageFunction and it can't be reloaded from Uri.

After this investigation it may be worth reading more about JournalEntry.KeepAlive property on MSDN.

This strategy helped me to find many memory-related insects. Hope it helps you too :).

PS: If you keep having problem with finding this particular leak, you could paste minimal code sample for us to reproduce it and give you more proper answer.

Cheers, Анвака

9
ответ дан 6 December 2019 в 14:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: