C# 文本框内存消耗
我有一个C# 文本框内存消耗,c#,wpf,xaml,C#,Wpf,Xaml,我有一个UserControl,它是一个项目列表,其中每个项目都包含许多TextBox和TextBlock控件。我尝试禁用撤消步骤,但即使在列表中添加10项,内存消耗也相当高。我试着运行内存分析工具,我可以看到文本框是大约50MB内存的最大消耗者 我的XAML如下所示 <UserControl.Resources> <converter:GenericButtonConverter x:Key="ButtonConverter"/> <DataTem
UserControl
,它是一个项目列表,其中每个项目都包含许多TextBox
和TextBlock
控件。我尝试禁用撤消步骤,但即使在列表中添加10项,内存消耗也相当高。我试着运行内存分析工具,我可以看到文本框是大约50MB内存的最大消耗者
我的XAML如下所示
<UserControl.Resources>
<converter:GenericButtonConverter x:Key="ButtonConverter"/>
<DataTemplate x:Key="RawSendListTemplate">
<ListBoxItem BorderBrush="LightGray" BorderThickness="0.5" HorizontalAlignment="Stretch" Margin="3,1,3,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="52"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="45"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="ID" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="1" Text="{Binding Path=CanId, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" TextWrapping="NoWrap" MaxLength="8"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1" />
<TextBlock Grid.Column="2" Text="CHANNEL" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="3" Text="{Binding Path=Channel, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" TextWrapping="NoWrap" MaxLength="1"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBlock Grid.Column="4" Text="Length" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="5" Text="{Binding Path=Len , UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="1"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<Button Grid.Column="6" Content="Send" x:Name="RawSendButton" IsEnabled="{Binding Path=SendButtonEnabled}" MinWidth="20" FontSize="11" FontWeight="SemiBold" Foreground="Black"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0"
Command="{Binding ButtonClick}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Name, Converter={StaticResource ButtonConverter}}"/>
<TextBlock Grid.Column="7" Text="CYCLE" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="8" Text="{Binding Path=CycleTimeMs, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=SendButtonEnabled}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="4"
VerticalAlignment="Center" TextAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<CheckBox Grid.Column="9" IsChecked="{Binding Path=CyclicEnableChecked, Mode=TwoWay}" IsEnabled="{Binding Path=CyclicEnable, UpdateSourceTrigger=PropertyChanged}" MinWidth="20"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,40,0"/>
<TextBlock Grid.Column="10" Text="DATA" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Left" Margin="0,0,10,0" />
<TextBox Grid.Column="11" Text="{Binding Path=Byte1}" IsEnabled="{Binding Path=Byte1Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
TextAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="12" Text="{Binding Path=Byte2}" IsEnabled="{Binding Path=Byte2Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="13" Text="{Binding Path=Byte3}" IsEnabled="{Binding Path=Byte3Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="14" Text="{Binding Path=Byte4}" IsEnabled="{Binding Path=Byte4Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="15" Text="{Binding Path=Byte5}" IsEnabled="{Binding Path=Byte5Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="16" Text="{Binding Path=Byte6}" IsEnabled="{Binding Path=Byte6Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="17" Text="{Binding Path=Byte7}" IsEnabled="{Binding Path=Byte7Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="18" Text="{Binding Path=Byte8}" IsEnabled="{Binding Path=Byte8Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
</Grid>
</ListBoxItem>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListBox x:Name="RawSendList" BorderThickness="1" BorderBrush="DarkGray" Margin="1" ItemsSource="{Binding MyViewModel.RawMessages}" ItemTemplate="{StaticResource RawSendListTemplate}"
SelectedItem="{Binding MyViewModel.SelectedRawMessageItem, Mode=OneWayToSource}" SelectionMode="Single" Background="OldLace"/>
</Grid>
现在我想知道我还能从哪里得到更多的钱为什么?内存很便宜。你有什么例外吗?如果没有,不要担心。分配是昂贵的,因此CLR倾向于保留任何分配的内存。放下任务管理器,走开。@WinterS那么您还有其他影响系统性能的因素。@WinterS可能是这些绑定。你需要看看你的C代码,找出问题所在。我保证问题不在XAML中。好吧,每个人都希望它是绑定,因为这就是他们所能看到的。显示您从内存分析器获得的报告。过去,我在网格中同时在屏幕上显示多个文本框时遇到了问题,对此问题有两种截然不同的解决方案。第一个问题是由设置
isundoabled=“False”
以禁用跟踪和存储TextBox.Text
更改引起的,并通过设置该选项加以解决。第二种解决方案是将所有内容显示为文本块
,这比文本框
占用的内存要少得多,并在用户编辑模板时将其更改为文本框
(例如单击该项)。
UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False"