单进程浏览器解决物理资源居高不下的极端方法。

现在手头上负责的项目是使用IE内核的WebBrowser组件做一个浏览器,支持多页面打开的。做完后在使用CHtmlView每打开的一个页面时浏览器所对应的进程的物理内存占用在逐渐递增,无可厚非,可在关闭时问题就出现了,之前页面所占用的物理内存空间并没有得到释放,使用时间短还好说,可是在一般的情况下一次打开就会使用上8小时左右,那可就惨了。

       在网上关于类似的问题大家都是怨声一片,在使用其它类型的浏览器找到了一种解决方案,就是利用多进程的机制来实现关闭页面时的资源回收。但是项目接近尾声,不能进行大改,所以这种解决方案暂时不适合这个项目。

       再后面有个大神给了一个解决方案,通过调用SetProcessWorkingSetSize(handle,-1,-1)方法来将当前进程中没被激活的页面丢到虚拟内存去处理,这使我眼前一亮,马上进行实现。在打开新页面与切换已打开页面时调用一次,发现果真能解决现在手头上棘手的问题。希望各位大牛能给出其它更好的解决方案。

BOOL SetProcessWorkingSetSize(

  HANDLE hProcess,
  SIZE_T 
dwMinimumWorkingSetSize,
  SIZE_T 
dwMaximumWorkingSetSize
);

这是从MSDN摘下的原话

Using the SetProcessWorkingSetSize function toset an application's minimum and maximum working set sizes does not guaranteethat the requested memory will be reserved, or that it will remain resident atall times. When the application is idle, or a low-memory situation causes ademand for memory, the operating system can reduce the application's workingset. An application can use the VirtualLock functionto lock ranges of the application's virtual address space in memory; however,that can potentially degrade the performance of the system.

使用这个函数来设置应用程序最小和最大的运行空间,只会保留需要的内存。当应用程序被闲置或系统内存太低时,操作系统会自动调用这个机制来设置应用程序的内存。应用程序也可以使用  VirtualLock 来锁住一定范围的内存不被系统释放。

When you increase the working set size ofan application, you are taking away physical memory from the rest of thesystem. This can degrade the performance of other applications and the systemas a whole. It can also lead to failures of operations that require physicalmemory to be present; for example, creating processes, threads, and kernelpool. Thus, you must use the SetProcessWorkingSetSize functioncarefully. You must always consider the performance of the whole system whenyou are designing an application.

当你加大运行空间给应用程序,你能够得到的物理内存取决于系统,这会造成其他应用程序降低性能或系统总体降低性能,这也可能导致请求物理内存的操作失败,例如:建立进程,线程,内核池,就必须小心的使用该函数。

 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值