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 6787373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:21:30+00:00 2026-05-26T17:21:30+00:00

My Application’s designed by MVVM. Main window is MainWindow.xaml as below. Main Window has

  • 0

My Application’s designed by MVVM.

Main window is MainWindow.xaml as below. Main Window has two usercontrols.

By the way, usercontrol is created dynamically in run-time, when property of viewmodel is changed.
But it seems my application has memory leak.

MainWindow.xaml

<Window x:Class="InstanceTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:InstanceTest"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:MainWindowViewModel x:Key="MainWindowViewModel" />

    <DataTemplate x:Key="UD1">
        <ContentControl>
            <local:UserControl1></local:UserControl1>
        </ContentControl>
    </DataTemplate>

    <DataTemplate x:Key="UD2">
        <ContentControl>
            <local:UserControl2></local:UserControl2>
        </ContentControl>
    </DataTemplate>

    <DataTemplate x:Key="contentsTemplate">
        <ContentControl>
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ViewType}" Value="1">
                            <Setter Property="ContentTemplate" Value="{DynamicResource UD1}" />
                        </DataTrigger>

                        <DataTrigger Binding="{Binding ViewType}" Value="2">
                            <Setter Property="ContentTemplate" Value="{DynamicResource UD2}" />
                        </DataTrigger>

                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>
    </DataTemplate>
</Window.Resources>
<Grid DataContext="{StaticResource MainWindowViewModel}">
    <StackPanel>
        <Button Height="30" Command="{Binding ChangeViewCommand}">ChangeView</Button>
        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource contentsTemplate}" />
    </StackPanel>
</Grid>

MainWindow.xaml is related to MainWindowViewModel.cs.
According to ViewType property, each usercontrol is created dynamically in run time.

public class MainWindowViewModel : ViewModelBase
{

    private string viewType;
    public string ViewType
    {
        get { return viewType; }
        set 
        {
            viewType = value;
            base.RaisePropertyChanged(() => this.ViewType);
        }
    }

    public MainWindowViewModel()
    {
        ChangeViewCommand = new RelayCommand(ChangeView);
    }

    public ICommand ChangeViewCommand { get; private set; }
    private void ChangeView(object o)
    {
        int aa = 1000;
        while (aa > 0)
        {
            System.Threading.Thread.Sleep(1000);

            System.Windows.Forms.Application.DoEvents();

            if (ViewType == "1")
                ViewType = "2";
            else
                ViewType = "1";
            aa--;

            //GC.Collect();
        }
    }
}

UserControl1.xaml

 <UserControl.Resources>
    <local:uc1ViewModel x:Key="uc1ViewModel" />
</UserControl.Resources>
<Grid DataContext="{StaticResource uc1ViewModel}">
    <StackPanel>
        <TextBlock FontSize="30" Text="{Binding TextExample}"></TextBlock>
        <Image Source="/InstanceTest;component/Images/sample.jpg" />
    </StackPanel>
</Grid>

UserControl1.xaml has uc1ViewModel.cs. UserControl2.xaml has uc2ViewModel.cs as below.

public class uc1ViewModel : ViewModelBase
{
    private string textExample;
    public string TextExample
    {
        get { return textExample; }
        set
        {
            textExample = value;
            base.RaisePropertyChanged(() => this.TextExample);
        }
    }

    public uc1ViewModel()
    {
        TextExample = "UD1...";
    }

    ~uc1ViewModel()
    {
        Debug.WriteLine("Call Destructor");
    }

}

ChangeView Function of MainViewModel.cs changes ViewType property every second.
When changing property of ViewType, ui1ViewModel is created as new instance. If ui1ViewModel’s instance is created for the next time, I expect old instance of ui1ViewModel is removed by garbage collector.

But, it looks like application has memory leak when testing for bunch of times.
I checked destructor of ui1ViewModel
and type Debug.WriteLine(“Call Destructor”) at destructor. but it dosen’t call every times.
When I call GC.Collect() by force, destructor of ui1ViewModel is called and reduce memory size of application.

So, my question is

Can I call GC.collect() by force in this case? or do you have other way to solve this problem?

  • 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-26T17:21:31+00:00Added an answer on May 26, 2026 at 5:21 pm

    I don’t think it is a memory leak either:

    A correctly-written program cannot assume that finalizers will ever
    run.

    Raymond Chen has a nice explanation on what people wrongly understand by Garbage Collection:
    http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx

    I’ll always remember this:

    Garbage collection is simulating a computer with an infinite amount of
    memory. The rest is mechanism.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application has an auxiliary thread. This thread is not meant to run all the
Application: WPF Application consisting of a textbox on top and a listbox below Users
Application.Current.MainWindow. ?
Our application has a lot of .NET assemblies, which up until now, has not
application = webapp.WSGIApplication( [(r'/main/profile/([a-f0-9]{40})', ProfileHandler)], debug=True) The regex in the above parameter will not
Application settings in Windows Forms is a convenient way to store application-wide properties. However,
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
WPF Application Shuts Down if I open a window and close it, but if
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
Application has a tabhost managed through TabActivity. It has option to add tabs at

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.