R – WPF Memory Usage #2

memoryperformancescrollwpf

I've asked a question about my memory management problem in WPF, but this is a different one about the same problem.
I am using DotTrace trying to figure out what's going on. When I start up my app, I can see in Task Manager that it is taking 200MB. DotTrace says a total of 33MB. If I simply minimize the window and bring it back up, the memory footprint according to TM is about 25MB.

I have a ListBox that shows peoples names and pictures. It shows up to 3000 people (I will work on paging, but that's not the point here). As I scroll down, I can see in TM that the memory increases rapidly. If I just keep scrolling up and down memory quick gets to 1GB. During the scroll there are no changes to the underlying data and there are no events of my own. If I minimize the window and bring it back up, memory drops from 1GB to 25MB.

Before minimizing and seeing the memory go down in TM I took a snapshot with DotTrace and it shows the same amount of memory as before the scrolling – around 30MB or so.

Can someone please explain to me what happens to memory when the app is minimized? Is the figure shown in Task Manager to be trusted?

Thanks

PS1:
There's no change in behavior if I do or don't add this to my ListBox:

VirtualizingStackPanel.IsVirtualizing="True" 
VirtualizingStackPanel.VirtualizationMode="Recycling"

PS2:
I've put a button with the code, and the GC doesn't reclaim much if anything(it drops from, say 700MB to 680MB):

    GC.Collect();
    GC.WaitForPendingFinalizers();

Best Answer

Can't give you a definitive answer, but some things to point out:

  1. DotTrace only shows managed memory usage. It looks your app is using large amounts of unmanaged memory (possibly allocated by WPF itself for all those images).

  2. Minimizing a process doesn't generally free any memory, it just pages it out so it can be used by other applications. It's a red herring in this case.

  3. Look into how .Net's generational garbage collection works.

  4. Until your process reaches the resource limits set by the OS it will not necessarily release any memory it has allocated to it.

  5. Use the Performance Counters built into Windows. They will show you a more useful overall breakdown of your app's memory than DotTrace, including managed and unmanaged memory.

  6. DotTrace sucks. Get ANTS or something. I love ReSharper, but Jetbrains really shouldn't charge for DotTrace, it's woeful.

Edit: 4) is a bit misleading - .Net's memory manager can release memory for other reasons (when a generation is full, for starters).

Related Topic