Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have a memory leak in my WPF Navigation?

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 http://img683.imageshack.us/img683/3013/ants.jpg

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?

like image 708
Brandon Avatar asked Dec 17 '09 22:12

Brandon


People also ask

Where are memory leaks found?

Where are memory leaks found? Explanation: Memory leaks happen when your code needs to consume memory in your application, which should be released after a given task is completed but isn't. Memory leaks occur when we are developing client-side reusable scripting objects.

How do you avoid memory leaks?

To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.

What is the main cause of memory leaks?

A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data. As a program operates, it sometimes needs more memory and makes an additional request.


1 Answers

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

like image 190
Anvaka Avatar answered Oct 03 '22 11:10

Anvaka