Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:00:06+00:00 2026-05-30T08:00:06+00:00

So I am using WPF 3.5 with MVVM + DataTemplate method to load 2

  • 0

So I am using WPF 3.5 with MVVM + DataTemplate method to load 2 views on the GUI. I have observed while memory profiling that items generated as part of items container of items controls are pinned into the memory and doesn’t get GCed even after the view is unloaded!

I just ran tests and found out it is reproducible even for the simplest of code… You guys can check for yourself.

XAML:

<Window x:Class="ContentControlVMTest.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ContentControlVMTest"
        Title="Window2" Height="300" Width="300">
    <DockPanel LastChildFill="True">

        <CheckBox Click="CheckBox_Click" Content="Test1?"
                  DockPanel.Dock="Top" Margin="5"/>

        <ContentControl x:Name="contentControl">
            <ContentControl.Resources>

                <DataTemplate DataType="{x:Type local:Test3}">
                    <TextBlock Text="{Binding C}" Margin="5"/>
                </DataTemplate>

                <DataTemplate DataType="{x:Type local:Test1}">
                    <DockPanel LastChildFill="True" Margin="5">
                        <TextBlock Text="{Binding A}"
                                   DockPanel.Dock="Top"
                                   Margin="5"/>
                        <ListBox ItemsSource="{Binding Bs}"
                                 DisplayMemberPath="B"
                                 Margin="5"/>
                    </DockPanel>
                </DataTemplate>
            </ContentControl.Resources>
        </ContentControl>
    </DockPanel>
</Window>

Code Behind:

public class Test3
{
    public string C { get; set; }
}

public class Test2
{
    public string B { get; set; }
}

public class Test1
{
    public string A { get; set; }

    private List<Test2> _Bs;
    public List<Test2> Bs
    {
        get
        {
            return _Bs;
        }

        set
        {
            _Bs = value;
        }
    }
}

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
        this.KeyDown += Window_KeyDown;
    }

    private void Window_KeyDown
            (object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.LeftCtrl))
            if (Keyboard.IsKeyDown(Key.LeftShift))
                if (Keyboard.IsKeyDown(Key.LeftAlt))
                    if (Keyboard.IsKeyDown(Key.G))
                    {
                        GC.Collect(2, GCCollectionMode.Forced);
                        GC.WaitForPendingFinalizers();
                        GC.Collect(2, GCCollectionMode.Forced);
                        GC.WaitForPendingFinalizers();
                        GC.Collect(3, GCCollectionMode.Forced);
                        GC.WaitForPendingFinalizers();
                        GC.Collect(3, GCCollectionMode.Forced);
                    }
    }

    private void CheckBox_Click(object sender, RoutedEventArgs e)
    {
        if (((CheckBox)sender).IsChecked.GetValueOrDefault(false))
        {
            var x = new Test1() { A = "Test1 A" };
            x.Bs = new List<Test2>();
            for (int i = 1; i < 10000; i++ )
            {
                x.Bs.Add(new Test2() { B = "Test1 B " + i });
            }
            contentControl.Content = x;
        }
        else
        {
            contentControl.Content = new Test3() { C = "Test3 C" };
        }
    }
}

I perform forced GC by Left Shift + Alt + Ctrl + G. All items for the Test1 or Test3 view and View Model gets dead after they are unloaded correctly. So that is as expected.

But the collection generated in the Test1 model (that has Test2 objects), remains pinned into the memory. And it indicates that the array is the one used by the items container of the listbox because it shows the number of de-virtualized items from the listbox! This pinned array changes it’s size when we minimize or restore the view in Test1 view mode! One time it was 16 items and next time it was 69 item when profiled.

enter image description here

This means WPF performs pinning of items generated in items controls! Can anyone explain this? Does this have any signficant drawback?

Thx a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:00:08+00:00Added an answer on May 30, 2026 at 8:00 am

    The problem is being caused by a failure of the binding mechanism to fully release the list items that have actually been bound for display on the screen. That last bit is almost certainly why you’re seeing different numbers of “orphaned” instances in different runs. The more you scroll the list, the more problems you generate.

    This seems to be related to the same sort of underlying problem as described in a bug report that I submitted over a year ago since the pinning root and pinned instance trees are similar. (To see that kind of detail in a convenient format, you might want to grab a copy of a somewhat fancier memory profiler like ANTS Memory Profiler.)

    The really bad news is that your orphaned instances are being pinned past the demise of the window itself, so you probably can’t clean them up without the same sort of hack I had to use in the WinForms scenario to force clean-up of the binding privates.

    The only bit of good news in all this is that the problem does not occur if you can avoid binding to nested properties. For example, if you add a ToString() override to Test2 to return the value of its B property and remove DisplayMemberPath from you listbox item, the problem will go away. e.g.:

    public class Test2
    {
        public string B { get; set; }
    
        public override string ToString()
        {
            return this.B;
        }
    }
    

    <ListBox ItemsSource="{Binding Bs}" 
        Margin="5"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a WPF app using MVVM. Most of my views have only xaml
I have a WPF application implemented using the MVVM framework that uses an ActiveX
I recently started using WPF and the MVVM framework, one thing that I have
So I have a check box that fires a command using WPF/MVVM this works
I have a WPF/MVVM (using MVVM-Light) app setup with a ComboBox that is inside
I notice that many of the WPF MVVM frameworks seem to avoid using the
Using the MVVM pattern creating WPF applications you have the ViewModel providing data to
I am using WPF MVVM Pattern. I have 2 ListBoxes and a DataGrid in
I have an application which I am developing using WPF\Prism\MVVM. All is going well
Using an MVVM approach to WPF, I have a view model class called SubButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.