MaterialDesignInXamlToolkit: Button styles: MaterialDesignRaisedButton used with MahApps.Metro causes serious memory leak

Bug explanation

I found a problem, maybe a compatibility issue or a design bug, but I think it can be fixed. My WPF program uses MahApps, (<mah:MetroWindow…), in which I use a button control of MaterialDesignInXamlToolkit, the style of this button control is: MaterialDesignRaisedButton. When I run the program, everything seems to be fine, but immediately after I minimize the form, the memory increases at a rate of 1MB/s, and it won’t stop, unless I restore this form that was just minimized, after restoring the memory immediately drop and return to normal levels. I know my question is about coming from two different places, (MahApps and MaterialDesignInXamlToolkit), but if I don’t use MahApps(mah:MetroWindow), then, the above problem doesn’t exist, everything works fine, also I tried several other Different styles of a similar button, only this style is found so far: MaterialDesignRaisedButton has this problem, the condition that triggers this problem is when the form is minimized.

**xaml code: <mah:MetroWindow x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel> <Grid Width="124"> <Button Margin="2,0" materialDesign:ButtonProgressAssist.IsIndeterminate="True" materialDesign:ButtonProgressAssist.IsIndicatorVisible="True" materialDesign:ButtonProgressAssist.Value="-1" Content="Indeterminate" IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Style="{StaticResource MaterialDesignRaisedButton}" /> </Grid> </StackPanel> </Grid> </mah:MetroWindow>

I have captured two pictures. Figure 1 shows that after running the WPF program and minimizing it, the change of memory transmission at this time can be seen to be increasing mah1

Figure 2 shows the memory changes after I restored the WPF program window that I just minimized. You can see that the memory has dropped rapidly and gradually returned to normal mah2

Version

4.7.1

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 25 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@AndrewKeepCoding Yes, I just tested your solution, setting the shadow effect to DP0, and it indeed solves the issue. materialDesignWpf:ElevationAssist.Elevation="Dp0"

@AndrewKeepCoding @gitjsdr26 Yes, this method does work: RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; However, from what I understand, setting the rendering mode to software rendering can potentially lead to a performance drop in terms of graphics, especially when dealing with complex graphics or animations. Sacrificing more for the sake of resolving a memory leak issue caused by a specific button animation might not align with the expectations of most projects. It would be ideal if the issue could be resolved while still allowing GPU acceleration.

This might be the reason that you can’t reproduce the memory leak.

After spending quite a bit of time trying to reproduce this the other day, I was very close to concluding that this must be a hardware-dependent (or at least system-dependent) issue. I think the issue you link to here is very interesting, and quite possible the issue we’re seeing. Could you, @shushu789, provide information about which graphics card you’re using. I am on NVIDIA GeForce which is likely why I can’t reproduce.

I also tried this on another computer (Win7 x64 English with Japanese IME installed) but didn’t reproduce the memory increase issue.

I think that this dotnet/wpf#7704 is somehow related to the issue. The computer that doesn’t reproduce the issue, has a NVIDIA GeForce GPU, whereas my laptop that does reproduce the issue, has an Intel Iris Xe Graphics GPU.

This might be the reason that you can’t reproduce the memory leak.

I totally missed @gitjsdr26 comment on ProcessRenderMode. It actually prevents the memory increasing.

RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;

I was able to reproduce this issue on a test app.

<mah:MetroWindow x:Class="Issue3065ReproApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Issue3065ReproApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes">
    <Grid>
      <Button
        materialDesign:ButtonProgressAssist.IsIndeterminate="True"
        materialDesign:ButtonProgressAssist.IsIndicatorVisible="True" />
    </Grid>
</mah:MetroWindow>

These are the facts that I’ve found so far:

  • The issue can be reproduced:
    • by just running the repro-app and minimize it
    • whether the keyboard input is on English or Japanese IME
  • The issue cannot be reproduced:
    • on the Sandbox environment
    • with a plain MainWindow
    • with just a ProgressBar without the Button
  • Memory increasing slows down with longer animation key frames.
  • Testing machine:
    • DELL XPS13
    • Windows 11 Pro 22H2
    • English by default
    • Japanese IME installed

I need to find some time to dig deeper so might take another few days. Since I’m not familiar with MaterialDesign, any tips are appreciated.

@nicolaihenriksen I’m having a similar issue, but I’m not using the MahApps.Metro library. There are also differences in the triggering method, see for details #3282

The problem looks like this: wpf301

thank you for your reply,I uploaded a demo project for this problem: https://github.com/shushu789/DotWPFDemo/tree/master