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
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
Version
4.7.1
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 25 (13 by maintainers)
@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.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.I was able to reproduce this issue on a test app.
These are the facts that I’ve found so far:
MainWindow
ProgressBar
without theButton
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:
thank you for your reply,I uploaded a demo project for this problem: https://github.com/shushu789/DotWPFDemo/tree/master