相关文章推荐
彷徨的热水瓶  ·  嬷_百度百科·  4 月前    · 
暴走的伤疤  ·  【书香机关】《八次危机:中国的真实经验(19 ...·  11 月前    · 
俊秀的牙膏  ·  从这四首诗词里看辛夷花的风姿·  11 月前    · 
安静的硬币  ·  Misfit - App on the ...·  1 年前    · 
深沉的台灯  ·  “慧眼”硬X射线调制望远镜_百度百科·  1 年前    · 
Code  ›  WPF 图片移除视觉树内存泄漏开发者社区
内存泄漏 wpf
https://cloud.tencent.com/developer/article/1584711
知识渊博的单车
2 年前
林德熙

WPF 图片移除视觉树内存泄漏

腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
林德熙
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > WPF 图片移除视觉树内存泄漏

WPF 图片移除视觉树内存泄漏

作者头像
林德熙
发布 于 2020-02-17 14:50:50
1K 0
发布 于 2020-02-17 14:50:50
举报
文章被收录于专栏: 林德熙的博客 林德熙的博客

本文告诉大家一个已知问题,在保存图片元素对象时,如果在图片移除视觉树之后再设置图片源为空,那么原有的图片源依然被图片元素引用不会释放

如写一个按钮,在点击事件里面创建 RenderTargetBitmap 加入到新建的图片元素,然后在下次点击事件时,将图片元素从视觉树移除之后设置图片源为空

        private void Button_Click(object sender, RoutedEventArgs e)
            // 每次点击此按钮会将当前呈现的图片移除视觉树,再将其Source属性设置为null。
            // 然后新建一个Image控件,并将其Source属性设置为RenderTargetBitmap对象,再呈现出来。
            // 再次过程中,RenderTargetBitmap对象从来不会被回收,造成内存泄露。
            // 可以从资源管理其中观察到程序的内存持续上涨的现象。
            // Remove the current Image control from the  visual tree and set source is null when click button.
            // Then new a image control and add source to the RenderTargetBitmap object and show it.
            // You can see the gc never delete the RenderTargetBitmap object that make  memory leak.
            var oldBorder = RootGrid.Children.OfType<Border>().LastOrDefault();
            if (oldBorder != null)
                var oldImage = (Image)oldBorder.Child;
                // 如果在Image控件移除视觉树之前将其Source属性设为null,并调用UpdateLayout方法。
                // 则RenderTargetBitmap对象可被回收,不会导致内存泄露。
                // 取消注释下面的代码可以观察到上述现象。
                // In order to solve it , you should set the image.Source is null and use UpdateLayout.
                // The below code can solve it.
                // oldImage.Source = null;
                // oldImage.UpdateLayout();
                // 将当前的Image控件移除视觉树。
                // Remove the current Image control from the  visual tree.
                RootGrid.Children.Remove(oldBorder);
                oldImage.Source = null;
                Borders.Add(oldBorder);
            var bitmap = new RenderTargetBitmap(1024, 1024, 96, 96, PixelFormats.Default);
            var image = new Image { Source = bitmap };
            var border = new Border { Child = image };
            RootGrid.Children.Add(border);
            // 为了便于观察内存的变化,每次操作后都会进行垃圾回收。
 
推荐文章
彷徨的热水瓶  ·  嬷_百度百科
4 月前
暴走的伤疤  ·  【书香机关】《八次危机:中国的真实经验(1949-2009)》书评
11 月前
俊秀的牙膏  ·  从这四首诗词里看辛夷花的风姿
11 月前
安静的硬币  ·  Misfit - App on the Amazon Appstore
1 年前
深沉的台灯  ·  “慧眼”硬X射线调制望远镜_百度百科
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号