惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs
小众软件
小众软件
D
Docker
博客园_首页
A
About on SuperTechFans
P
Privacy International News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
Arctic Wolf
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Latest news
Latest news
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
K
Kaspersky official blog
V2EX - 技术
V2EX - 技术
SecWiki News
SecWiki News
U
Unit 42
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LINUX DO - 热门话题
S
Security Affairs
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
T
Tenable Blog
W
WeLiveSecurity
Cloudbric
Cloudbric
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
N
News and Events Feed by Topic
D
DataBreaches.Net
P
Proofpoint News Feed
B
Blog RSS Feed
B
Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org

博客园 - 思无邪

Silverlight内存泄露(八)样式 Silverlight内存泄露(六)MEF等Ioc框架引起内存泄露-ExportLifetimeContext Silverlight内存泄露(五)MEF等Ioc框架引起内存泄露-PartCreationPolicy Silverlight内存泄露(四)解决内存泄露 Silverlight内存泄露(二)检测内存泄露 从火狐放弃依据特性更新版本说起 Silverlight内存泄露(二)解决内存泄露之Dispose误用 Silverlight内存泄露(一)序 Silverlight IReader阅读器第二版 silverlight阅读器——面向领域的浏览器(二)——Silverlight阅读器架构 silverlight阅读器——起源面向领域的浏览器——概念 silverlight异步陷阱(二)循环 silverlight 异步陷阱(一)不能Remove事件处理程序 IReader Silverlight电子阅读器介绍开源项目 silverlight在线阅读器(二):为silverlight增加gb2312编码 silverlight在线阅读器(一):介绍 新建立一个类似于CuteEditor的项目,希望有人参加。发布一个测试版本的dll 从控件开发的角度看几个editor控件,Freetextbox,radtoolbar,abouteditor,cuteeditor 想做一个关于word解析和HtmlEditor的项目,希望有人加入 cuteEditor6.0的破解方式与Cuteeditor6.0的脚本调试
Silverlight内存泄露(七)Command
思无邪 · 2011-04-17 · via 博客园 - 思无邪

MVVM中View与ViewModel是强引用的双向关联关系,容易发生内存泄露,任何一方没有被回收都会导致另一方不能被GC回收。

Comand连接了View与ViewModel,容易产生内存泄露。

发现内存泄露

在几个View间导航几次,两次导航到View页面,获取内存快照,发现Info有两个实例。内存没有被释放。

为了每次导航到消息页面,都保存上一次显示的信息,InfoViewModel采用了单例模式,作为缓存,InfoViewModel只有一个实例,按照设想Info也应该只有一个实例。

代码:

var lazyViewModelMapping= LazyViewModelExports.FirstOrDefault(o => o.Metadata.Key.Equals(relativeUri.Host, StringComparison.OrdinalIgnoreCase));

viewModel = lazyViewModelMapping.Value;

var viewFactory = viewMapping.CreateExport();

view = viewFactory.Value as Control;

viewFactory.Dispose();//释放View内存

view.DataContext = viewModel;

是什么原因造成viewFactory.Dispose();没有释放View的内存呢?

clip_image002

确定泄露原因

看下面两张Info图:

正常图:

image

发生泄露图:View是当前页面:

commandLeak

发生泄露图:View不是当前页面:

commandLeak2

可看出stopDownloadCommand在View与ViewModel间建立了强引用关系,遇到ViewModel这个单例模式(生存期与应用程序一样)造成View不能被回收。

解决内存泄露

断开stopDownloadCommand内存得到释放,从上图可看出断开这个command并没有从根本解决问题,增加其他Command仍会产生问题。

clip_image004

根本原因是ViewModel在此不应该使用单例模式,Lazy创建了单例,而应该缓存Modle。

弹出窗口是最好的方式,由于Silverlight4不支持子窗口,在此使用单例作为缓存,没想到造成View不能被释放。

总结

a) 绑定View的类(如VM),使用单例或缓存VM需要谨慎。VM的长生命周期将导致View不能被释放,并且每次导航到View,View的实例数都将增加。

b) 从内存角度看单例,单例中的Command、Event非常容易造成内存泄露,根源往往是单例而不是事件或command。

c)ANTS 展现的内存图并不全面,经常需要多张图结合看问题。比如,当View是当前页面时,不能看出Lazy<T>产生的问题,而需要跳转到View不是当前页面再看另外的快照。

d)ANTS 的快照图,对一类引用进行了归类,比如stopDownloadCommand并不是ViewModle中唯一的Command,还有reDownloadCommand也引用了View,图上并没有表现出来,在代码中修正了stopDownloadCommand的引用问题,View仍被reDownloadCommand引用而不能释放。