views:

199

answers:

2

I'm looking through a WPF application looking for a memory leak (using ANTS Memory Profiler 5.1) and I keep seeing some pages and controls taking up memory when they shouldn't be.

So I go to the Object Retention Graph and to see what is keeping them around, and I keep seeing this for every page:

Object Retention Graph

The thing is, I have KeepAlive set to false on every page, and I don't think such a property exists on the user controls.

Can anyone tell me what I should be looking for? Is this even a memory leak or is this normal behaviour for a WPF application?

A: 

This article is so unbelievably helpful on this subject: Finding Memory Leaks in WPF-based applications.

Jason
Thakns for the link Jason, but I've already been through that guide. It's help me fixed quite a few other leaks, but my problem is that this leak doesn't seem to fit the criteria of anything covered in that guide. unless I'm overlooking something? I'm not that familiar with how the NavigationService actually works.
Brandon
+2  A: 

Hi Brandon,

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, Anvaka

Anvaka
Thank you for the detailed explanation, but I'm not sure I follow the DependencyProperty part. If I've set the KeepAlive to be false on every page, why would it still be kept in memory?
Brandon
Follow the logical path: even if you set KeepAlive=false on every page, you still can get true from IsContentKeepAlive() when flag2 is true, which says: !CanReloadFromUri
Anvaka
Ah, right. Thanks for the great answer.
Brandon
:) I'm glad I could help! Cheers
Anvaka