WPF简单的用户控制消耗超过1.4 GB的内存

发布于 2025-01-30 19:32:26 字数 7146 浏览 2 评论 0原文

我开始学习WPF C#,并通过用户控制制作了一个简单的应用程序,但注意到此简单应用程序从内存中消耗了超过1.4 GB。 那是正常的

<UserControl x:Class="newShooting.user_controls.roundButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:newShooting.user_controls"
         mc:Ignorable="d" d:DesignWidth="100" Height="230">

<Grid Height="230" >
    <Border Background="#FF53A5ED" ></Border>
    <StackPanel Background="{Binding bkground}">
        <!--<StackPanel.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF715757" Offset="1"/>
                </LinearGradientBrush>
            </StackPanel.Background>-->
        <Label HorizontalAlignment="Center" Foreground="AntiqueWhite" >
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF20A3DA" Offset="1"/>
                </LinearGradientBrush>
            </Label.Background> 1
        </Label>
        <Image x:Name="imageBody" Height="100" Width="100" Source="/user_controls/png-clipart-shooting-target-graphy-bullet-shooting-range-others-miscellaneous-human.png"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="4*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="bullets" Grid.Column="1"  FontSize="72" FontWeight="Bold" Foreground="#FFDA2020" FontFamily="Arial Rounded MT Bold" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontStretch="UltraExpanded">1</TextBlock>
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="2*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <Image x:Name="light" Source="/user_controls/Lovepik_com-400229766-light-bulb.png" Grid.Row="0"></Image>
                <Ellipse x:Name="connected"  Height="15" Width="15" StrokeThickness="5" Fill="#FFF70202" Grid.Row="2" />
            </Grid>
        </Grid>
        <ComboBox x:Name="cb_shooters"></ComboBox>
    </StackPanel>

</Grid>
this is the user control and the follwoing will be the main window .
<Window x:Class="newShooting.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:newShooting"
    xmlns:uc="clr-namespace:newShooting.user_controls"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" WindowState="Maximized"
    Background="Transparent" WindowStyle="None"
    >

<Grid >
    <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
    </Grid.RowDefinitions>
    <Border Background="DimGray" Grid.ColumnSpan="10" Grid.RowSpan="3">
    </Border>
    <Button Content="Button" Grid.Column="1" Grid.Row="2" Click="Button_Click"/>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="0" Grid.Row="1">
        <uc:roundButton x:Name="UC1" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="1" Grid.Row="1">
        <uc:roundButton x:Name="UC2" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="2" Grid.Row="1">
        <uc:roundButton x:Name="UC3" VerticalAlignment="Center" bkground="#FF1E598C" ></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="3" Grid.Row="1">
        <uc:roundButton x:Name="UC4" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="4" Grid.Row="1">
        <uc:roundButton x:Name="UC5" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="5" Grid.Row="1">
        <uc:roundButton x:Name="UC6" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="6" Grid.Row="1">
        <uc:roundButton x:Name="UC7" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="7" Grid.Row="1">
        <uc:roundButton x:Name="UC8" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="8" Grid.Row="1">
        <uc:roundButton x:Name="UC9" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="9" Grid.Row="1">
        <uc:roundButton x:Name="UC10" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
</Grid>

,那么内存的用法是:

“内存使用”

是正常的,是否有一种减少内存消耗的方法? 笔记: 我尝试添加gc.collect(),但不起作用。 我应该怎么办 。

i 'm starting to learn wpf c# and made a simple application with user control but noticed that this simple application consumes more than 1.4 GB from memory .
is that normal

<UserControl x:Class="newShooting.user_controls.roundButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:newShooting.user_controls"
         mc:Ignorable="d" d:DesignWidth="100" Height="230">

<Grid Height="230" >
    <Border Background="#FF53A5ED" ></Border>
    <StackPanel Background="{Binding bkground}">
        <!--<StackPanel.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF715757" Offset="1"/>
                </LinearGradientBrush>
            </StackPanel.Background>-->
        <Label HorizontalAlignment="Center" Foreground="AntiqueWhite" >
            <Label.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF20A3DA" Offset="1"/>
                </LinearGradientBrush>
            </Label.Background> 1
        </Label>
        <Image x:Name="imageBody" Height="100" Width="100" Source="/user_controls/png-clipart-shooting-target-graphy-bullet-shooting-range-others-miscellaneous-human.png"/>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="4*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="bullets" Grid.Column="1"  FontSize="72" FontWeight="Bold" Foreground="#FFDA2020" FontFamily="Arial Rounded MT Bold" HorizontalAlignment="Center" VerticalAlignment="Stretch" FontStretch="UltraExpanded">1</TextBlock>
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="2*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <Image x:Name="light" Source="/user_controls/Lovepik_com-400229766-light-bulb.png" Grid.Row="0"></Image>
                <Ellipse x:Name="connected"  Height="15" Width="15" StrokeThickness="5" Fill="#FFF70202" Grid.Row="2" />
            </Grid>
        </Grid>
        <ComboBox x:Name="cb_shooters"></ComboBox>
    </StackPanel>

</Grid>

this is the user control and the follwoing will be the main window .

<Window x:Class="newShooting.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:newShooting"
    xmlns:uc="clr-namespace:newShooting.user_controls"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" WindowState="Maximized"
    Background="Transparent" WindowStyle="None"
    >

<Grid >
    <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
        <RowDefinition Height="2*"></RowDefinition>
    </Grid.RowDefinitions>
    <Border Background="DimGray" Grid.ColumnSpan="10" Grid.RowSpan="3">
    </Border>
    <Button Content="Button" Grid.Column="1" Grid.Row="2" Click="Button_Click"/>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="0" Grid.Row="1">
        <uc:roundButton x:Name="UC1" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="1" Grid.Row="1">
        <uc:roundButton x:Name="UC2" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="2" Grid.Row="1">
        <uc:roundButton x:Name="UC3" VerticalAlignment="Center" bkground="#FF1E598C" ></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="3" Grid.Row="1">
        <uc:roundButton x:Name="UC4" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="4" Grid.Row="1">
        <uc:roundButton x:Name="UC5" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="5" Grid.Row="1">
        <uc:roundButton x:Name="UC6" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="6" Grid.Row="1">
        <uc:roundButton x:Name="UC7" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="7" Grid.Row="1">
        <uc:roundButton x:Name="UC8" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="8" Grid.Row="1">
        <uc:roundButton x:Name="UC9" VerticalAlignment="Center" bkground="#FF1E598C"></uc:roundButton>
    </StackPanel>
    <StackPanel VerticalAlignment="Center" Orientation="Vertical" Grid.Column="9" Grid.Row="1">
        <uc:roundButton x:Name="UC10" VerticalAlignment="Center"></uc:roundButton>
    </StackPanel>
</Grid>

then the memory usage is :

memory usage

is that normal and are there a way to reduce the memory consumption ?
Note:
i tried to add GC.collect() but doesn't work .
what should i do .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文