WPF内存泄漏

2023-11-23

我有一个简单的 wpf 应用程序。在主窗口中,我有堆栈面板和 2 个按钮。第一个按钮添加 100 个我的用户控件(没有任何数据绑定、事件、位图),第二个按钮从面板中删除所有控件并调用 GC.Collect()。并且存在一些问题: 1.当我第一次点击“删除”按钮后,并不是所有的内存都被释放,我必须点击几次才能释放更多的内存。 2. 5 - 10 分钟后内存释放,但几兆字节没有释放。

例如,我的应用程序启动后大约需要 22mb 当我添加 500 个控件时 - ~60mb 我第一次点击“删除”按钮后 - ~55mb(我等待了一段时间,内存没有释放) 我点击了几次,内存下降到25MB, 我不明白这一点,我是 WPF 的新手,也许我错过了一些东西 我想立即释放内存。

<Window x:Class="WpfApplication10.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="385" Width="553">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition Height="240*" />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>
    <Grid 
            Name="border1" 
            Grid.Row="1"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch" >
        <ScrollViewer VerticalAlignment="Stretch"
                      Name="scrollViewer1" 
                      HorizontalAlignment="Stretch">
            <StackPanel 
                Margin="3,3,3,3"
                Background="Transparent"
                VerticalAlignment="Stretch"
                Name="activityStackPanel"
                HorizontalAlignment="Stretch">
            </StackPanel>
        </ScrollViewer>
    </Grid>
    <Button Content="Button" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="12,0,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Button Content="Button" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="141,0,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    <Label Content="Label" Grid.RowSpan="2" Height="28" HorizontalAlignment="Left" Margin="34,0,0,0" Name="label1" VerticalAlignment="Top" />
</Grid>

namespace WpfApplication10
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int N = 100;
            //var r = new ActivityStatisticItem("111", "222", DateTime.Now, "333", 1);
            for (int i = 0; i < N; i++)
            {
                activityStackPanel.Children.Add(new UserControl1());
            }

            label1.Content = activityStackPanel.Children.Count;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
           activityStackPanel.Children.Clear();

            label1.Content = activityStackPanel.Children.Count;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}
<UserControl x:Class="WpfApplication10.UserControl1"
      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" 
         Background="Transparent"
         Margin="0,2,0,2"
         MinHeight="80"
         MinWidth="130"
         MaxHeight="80">
<Grid Width="441">
    <Grid.RowDefinitions>
        <RowDefinition Height="40" Name="rowTop" />
        <RowDefinition Height="40" Name="rowBottom"/>
    </Grid.RowDefinitions>
    <Border BorderBrush="Gray" 
            BorderThickness="1" 
            HorizontalAlignment="Stretch" 
            Background="LightGreen"
            Name="contactPanel" 
            CornerRadius="3,3,3,3"
            VerticalAlignment="Stretch" Panel.ZIndex="1" >
        <Grid
            VerticalAlignment="Stretch" 
            Name="grid1" 
            Margin="3,0,3,0"
            HorizontalAlignment="Stretch">

            <Label Content="Contact" Height="15" HorizontalAlignment="Left" Margin="15,3,0,0" Name="headerLabel" Padding="0" VerticalAlignment="Top" FontSize="10" FontWeight="DemiBold"/>
            <Label Content="00/00/0000 00:00:00" Height="15" HorizontalAlignment="Left" Margin="13,18,0,0" Name="timeLabel" Padding="0" VerticalAlignment="Top" FontSize="10" Width="100" FontWeight="DemiBold" />
            <Label Content="00:00:00" Height="15" HorizontalAlignment="Right" Margin="0,18,0,0" Name="durationLabel" Padding="0" VerticalAlignment="Top" FontSize="10" Width="38" FontWeight="DemiBold"/>

            <!--<Image Height="12" HorizontalAlignment="Left" Margin="0,3,0,0" Name="directionPictureBox" Stretch="Fill" VerticalAlignment="Top" Width="12"  />
            <Image Height="12" HorizontalAlignment="Right" Margin="0,20,41,0" Name="timerImage" Stretch="Fill" VerticalAlignment="Top" Width="12"          />
            <Image Height="12" HorizontalAlignment="Left" Margin="0,20,0,0" Name="dateTimeImage" Stretch="Fill" VerticalAlignment="Top" Width="12"       />-->


        </Grid>
    </Border>
    <Border BorderBrush="Gray" 
            BorderThickness="1,0,1,1" 
            Grid.Row="1" 
            Background="White"
            HorizontalAlignment="Stretch" 
            Margin="10,0,10,0" 
            Name="detailsPanel" 
            CornerRadius="0,0,3,3"
            VerticalAlignment="Stretch">
        <Grid HorizontalAlignment="Stretch" 
              Name="grid2" 
              Margin="3,0,3,0"
              VerticalAlignment="Stretch">
            <Label Content="Label" Height="15" HorizontalAlignment="Stretch" FontSize="9" Padding="0" Margin="0,3,0,0" Name="numberRadLabel" VerticalAlignment="Top" />
            <Label Content="Label" Height="15" HorizontalAlignment="Stretch" FontSize="9"  Padding="0" Margin="0,18,0,0" Name="queueRadLabel" VerticalAlignment="Top" />

        </Grid>
    </Border>
</Grid>

在用户控制中我只有

         public UserControl1()
         {
            InitializeComponent();
         }

我想立即释放内存。

不。相信GC。

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

不。相信GC。

5 - 10 分钟后内存释放

我不是说相信GC吗?


  • 垃圾收集模型将确保不需要的managed系统中的内存被释放(其中包括几乎所有控件内存)。它使用一种优化算法,其中包括生成、可用的空闲内存、可能的 CPU 可用......所以GC.Collect()会干扰它。

  • GC.Collect()是异步的,所以不会立即生效。

  • 您需要小心的唯一资源是非托管资源,通常由处置模式。否则就不要搞乱GC,它的工作做得很好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF内存泄漏 的相关文章

  • 如何向WebRequest添加参数?

    我需要从 Web 服务调用一个方法 所以我编写了以下代码 private string urlPath http xxx xxx xxx manager string request urlPath index php org get or
  • C++ 中的“int”默认是“signed long int”吗?

    Is int默认情况下signed long int in C 它是否依赖于平台和 或编译器 如果是这样 怎么办 EDIT 以下任何一项是否保证是重复的 signed short int signed int signed long int
  • C++0x 初始值设定项列表示例

    我想看看这个现有代码示例如何利用 C 0x 初始化列表功能 示例0 include
  • std::bind2nd 和 std::bind 与二维数组和结构数组

    我知道 C 有 lambda 并且 std bind1st std bind2nd 和 std bind 已弃用 然而 从C 的基础开始 我们可以更好地理解新特性 所以 我从这个非常简单的代码开始 使用int 数组s 第一个例子 与std
  • c 使用 lseek 以相反顺序复制文件

    我已经知道如何从一开始就将一个文件复制到另一个文件 但是我如何修改程序以按相反的顺序复制它 源文件应具有读取访问权限 目标文件应具有读写执行权限 我必须使用文件控制库 例如 FILE A File B should be ABCDEF FE
  • PartialView Action 正在调用自身

    我有 MVC 应用程序 它用于从主视图 ProductMaster 将 ProductAreaGrid 列表显示为 PartialView 并且它将在局部视图内将 CreateProductArea 作为 PartialView 我的 Gr
  • += 运算符在 C++ 中是如何实现的?

    这是我一直在思考的一个问题 但从未找到任何资源来说明这个问题的答案 事实上它不仅是为了 也适用于它的兄弟姐妹 即 等等 当然不是 考虑这个例子 int a 5 a 4 this will make a 9 现在考虑等效表达式 a a 4 T
  • 根据 Active Directory 策略检查密码[重复]

    这个问题在这里已经有答案了 我有一个允许用户更改其 AD 密码的前端 有没有办法获取特定用户及其属性 长度 复杂性 的密码策略 例如细粒度 有没有办法根据此特定策略检查字符串 xyz121 编辑 我不想检查活动目录中存储的当前密码 我想检查
  • 使用 catch all 字典属性将 json 序列化为对象

    我想使用 JSON net 反序列化为对象 但将未映射的属性放入字典属性中 是否可以 例如给定 json one 1 two 2 three 3 和 C 类 public class Mapped public int One get se
  • 如何在不修改以前的文本的情况下更改 WPF RichTextBox 中的 FontFamily

    当您使用 RichTextBox 的 FontFamily 属性时 它会更改 FlowDocument 内整个内容的 FontFamily 就像执行 EditingCommands ToggleBold 这样的命令一样 它仅更改插入符号下的
  • C# 反序列化过程中创建指向父对象的指针

    我有这样的课程 Serializable public class child public Parent parent Serializable public class Parent public List
  • 如何解决文件被另一个进程使用的问题?

    我一直在 VS NET 2010 中调试 没有任何问题 但现在无法建造 我收到错误 Unable to copy file filename to bin Debug filename The process cannot access t
  • 这些工作队列标志意味着什么?

    在研究工作队列时 我遇到了内核中定义的工作队列标志和常量 我有以下我无法理解的疑问 这里的排水和救援到底是什么意思 WQ DRAINING 1 lt lt 6 internal workqueue is draining WQ RESCUE
  • 如何在 XAML 中使用其他项目的图像?

    我正在构建一个包含多个项目的解决方案 Windows Phone 应用程序 其中一个项目是用于 品牌 的 它包含一些特定的代码和图像 其想法是该项目可以更换为不同的品牌 我的应用程序页面位于主项目中 我想在主应用程序的 UI 中显示存储在第
  • WPF 和 ClickOnce

    MSDN 未将 WPF exe 列为 ClickOnce 支持的应用程序类型 ClickOnce 应用程序是任何 Windows Presentation Foundation xbap Windows 窗体 exe 控制台应用程序 exe
  • 使用 WinAPI 连接禁用的显示设备

    我的问题是启用禁用的监视器ChangeDisplaySettingsEx 我想这不是火箭科学 但经过一番挖掘后 它看起来仍然是不可能的 我找到了一种根据找到的 Microsoft 代码示例禁用所有辅助显示器的方法here https msd
  • 在 lua 中加载 C++ 模块时出现“尝试索引字符串值”错误

    我正在尝试使用 lua 用 C 编写的函数 下面给出的是cpp文件 extern C include lua h include lauxlib h include lualib h static int add 5 lua State L
  • 在 C# 窗口应用程序中运行 C/C++ 控制台应用程序?

    现在 我想开发一个简单的应用程序 因此我决定最快的编码方式是 C NET 但现在 我很难实现我需要的功能之一 我想做的是在 C 应用程序的窗口内运行 C C 控制台应用程序 就像在虚幻前端中一样 添加一点通信方式 以便我可以为控制台应用程序
  • 使用方法的状态模式

    我正在尝试使用方法作为状态而不是类来基于状态模式的修改版本来实现一个简单的状态机 如下所示 private Action
  • 查找和替换正则表达式问题

    感谢这里对我其他问题的所有大力帮助 我开始掌握正则表达式 但我仍然对这个一无所知 我的代码是 StreamReader reader new StreamReader fDialog FileName ToString string con

随机推荐

  • 在 IIS Express 中托管 WCF 服务时出现问题

    我们正在尝试在 IIS Express 7 5 7 5 1046 中托管 WCF 服务 IIS Express 正确启动 但在尝试连接到 svc 文件时 出现异常 并显示消息 无法加载 DLL nativerd dll 找不到指定的模块 来
  • “numpy.ndarray”对象没有属性“read”

    我试图将函数中声明的变量用于另一个函数 但是当我这样做时 我收到了这样的错误 Exception in Tkinter callback Traceback most recent call last File C Users HP App
  • 使用 RegEx 查找两个 XML 标记之间的所有内容

    In RegEx 我想找到标签以及两者之间的所有内容XML tags 如下所示
  • 类型错误:firebase.storage 不是函数

    下列的this例如 我不断收到错误 TypeError firebase storage is not a function 从我的代码中的这一行 var storageRef firebase storage ref 当我只是尝试从存储指
  • 源文件中的 Swift 编辑器占位符

    您好 遇到快速错误 源文件中的 Swift 编辑器占位符 问题 这是我的代码 public func collectionView collectionView UICollectionView cellForItemAt indexPat
  • JPA/Hibernate 连接恒定值

    我试图在连接语句中使用常量值连接到同一个表中的不同实体 在 SQL 中 我会做这样的事情 SELECT FROM owner o JOIN types t on t owner id o id AND t type A THIS IS WH
  • Web API 以 XML 形式返回 OAuth 令牌

    使用具有单个用户帐户的默认 Visual Studio 2013 Web API 项目模板 并使用 application xml 的 Accept 标头发布到 token 端点 服务器仍然以 JSON 形式返回响应 access toke
  • 打开简历错误:(-215) scn == 3 ||函数 cvtColor 中的 scn == 4

    我目前使用的是 Ubuntu 14 04 使用 python 2 7 和 cv2 当我运行这段代码时 import numpy as np import cv2 img cv2 imread 2015 05 27 191152 jpg 0
  • C/C++ 中字符 ('a') 的大小

    C 和 C 中字符的大小是多少 据我所知 C 和 C 中 char 的大小都是 1 个字节 In C include
  • SQL Server:从列到行

    寻找优雅的 或任何 解决方案将列转换为行 这是一个示例 我有一个具有以下架构的表 ID EntityID Indicator1 Indicator2 Indicator3 Indicator150 这是我想要得到的结果 ID EntityI
  • WPF 按钮内的按钮点击问题

    我的 WPF 项目具有以下结构 非常简化 Button newProduct new Button Grid newGrid new Grid Button modify new Button Button remove new Butto
  • 修复 - System.Net.WebException:远程服务器返回错误:(500) 语法错误,命令无法识别

    我创建了 FTP 代码来传输文件 这段代码工作正常 只是有时会导致错误 500 确切的错误是 Error System Reflection TargetInvocationException Exception has been thro
  • Backbone.js PushStates:Internet Explorer 的后备功能不起作用

    我的网站刚刚在 Backbone js 中实现了推送状态 整个网站在 IE 下都崩溃了 我应该如何为 IE 创建后备 我想要实现的目标 主要网址 http mydomain com explore 另一个网址 http mydomain c
  • 通过 JavaScript 动态添加的元素上的 CSS 转换 [重复]

    这个问题在这里已经有答案了 我用这样的方式创建一个元素 var dynamic gallery document createElement li 现在我给它分配一个类 它给出了元素样式a height 0 transition durat
  • 如何将工作项从一个组织移动到另一个组织

    我们在一个项目中有许多工作项 现在 我们的 DevOps 中有另一个组织 我们希望将所有现有的工作项从旧组织 项目 移动到新组织 如何才能做到这一点 我见过人之前讨论过这个 还有一些评论说 我们使用excel 但没有关于如何实际执行此操作的
  • 如何在部署过程中自动分发所需状态配置自定义资源?

    我正在努力利用 Microsoft 的 DSC 资源工具包 特别是 XWebAdministration 至少对于初学者而言 我对 DSC 比较熟悉 所以不用担心脚本的实际功能 它做了它应该做的事情 或者至少我很确定它做了 问题是 当我从编
  • cron 作业可以每“x”秒运行一次吗

    我有一个 cron 作业设置 最小值为 60 秒 我希望程序能够以秒为间隔运行 即无论我将其设置为 60 秒以后 例如 我希望 cron 作业每 65 秒运行一次 或每 63 秒运行一次 或每 160 秒运行一次 等等 这可能吗 或者 cr
  • 在 Python 中构建最小的插件架构

    我有一个用 Python 编写的应用程序 由相当技术性的受众 科学家 使用 我正在寻找一种使用户可扩展应用程序的好方法 即脚本 插件架构 我在找东西极轻 大多数脚本或插件不会由第三方开发和分发并安装 而是由用户在几分钟内创建以自动执行重复任
  • MessageDigest NoSuchAlgorithmException

    我想用MessageDigest获取 MD5 哈希值 但出现错误 import java security MessageDigest public class dn public static void main String args
  • WPF内存泄漏

    我有一个简单的 wpf 应用程序 在主窗口中 我有堆栈面板和 2 个按钮 第一个按钮添加 100 个我的用户控件 没有任何数据绑定 事件 位图 第二个按钮从面板中删除所有控件并调用 GC Collect 并且存在一些问题 1 当我第一次点击