.NET WebBroswer内存释放
2015-07-19 10:37
459 查看
最近写的小说爬取工具遇到了性能瓶颈,使用多个Webbroswer控件预加载多个网页,内存会不断增加,达到400M左右,不能忍。
首先尝试把Webbroswer对象置为null,wb=null,等待GC自动回收,毫无效果。
而后尝试加入一个强制垃圾回收
依然无效
首先在类的开头导入系统内核
在释放浏览器内存时调用
值得注意的是,该方法不宜经常使用
它带来一个很严重的弊端,操作系统为了实现限制内存的大小,会不断的进行内存与虚拟内存之间的转换,反而大大加重了操作系统的负担,所以不宜常用。
失败的例子
首先尝试把Webbroswer对象置为null,wb=null,等待GC自动回收,毫无效果。
WebBrowser wb = new WebBrowser(); wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs args) { if (wb != null && IsBroswerOK(wb)) { Html = wb.DocumentText; //删除浏览器 wb = null; } }; wb.Navigate(c.Url);
而后尝试加入一个强制垃圾回收
System.GC.Collect();
依然无效
成功的例子
果然自己还是靠不住,老老实实开谷歌搜,终于在MSDN上找到解决方案。原帖点我首先在类的开头导入系统内核
[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)] internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize); [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)] internal static extern IntPtr GetCurrentProcess();
在释放浏览器内存时调用
//删除浏览器 wb = null; //清理内存 IntPtr pHandle = GetCurrentProcess(); SetProcessWorkingSetSize(pHandle, -1, -1);
值得注意的是,该方法不宜经常使用
它带来一个很严重的弊端,操作系统为了实现限制内存的大小,会不断的进行内存与虚拟内存之间的转换,反而大大加重了操作系统的负担,所以不宜常用。
相关文章推荐
- Ogre参考手册(十二) 8 动画
- 复制控制----句柄类与继承
- 【猪猪-后端】WebMagic框架搭建的爬虫,根据自定义规则,直接抓取,使用灵活,Demo部署即可查看。
- QTP中运行错误的捕捉和记录
- Ogre参考手册(十一) 7 Shadows阴影
- [转载] 新浪微博MySQL优化的小结和反思
- c中sizeof()运算符的问题
- 每天一个小知识点6(bootstrap总结)
- 消除Java 的冗余代码
- Hadoop伪分布式与集群式安装配置
- 移动开发构架漫谈——反劫持实战篇
- Hadoop—HDFS读写文件操作---练习4
- Android中handler的使用及原理---学习笔记
- 猜图游戏
- Android中handler的使用及原理---学习笔记
- 第一个iOS APP上架经历
- 不同VLAN之间通信的原理
- Web学习之HTML
- 程序员面试资源集锦
- POJ1852 Ants