CEF内存增加导致崩溃或白屏?

自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

https://www.cnblogs.com/bclshuai/p/11380657.html

1.问题描述

基于CEF(Chromium Embedded Framework)做了一个客户端网页浏览器,用于加载一些网页,有个车辆查询的网页,有很多的图片要加载显示,图片是分页展示的,每翻一页,内存就会增加一点,增加到800M时,进程会崩溃或者白屏。

2.问题分析

内存增加导致进程崩溃。

(1)CEF定时清除缓存

(2)前端控制翻页时清除上一页的图片数据

(3)增大CEF的最大内存上限。

 

3.解决办法

3.1设置vs2015大缓存编译属性---实测无效

 

 3.2CefSettings settings设置本地缓存路径----实测无效

CefString(&settings.cache_path).FromString(strDumpPath);
CefString(&settings.root_cache_path).FromString(strDumpPath);

按照下面的参数说明,如果没有设置缓存路径则会在内存中进行缓存。

///
// The location where data for the global browser cache will be stored on
// disk. If this value is non-empty then it must be an absolute path that is
// either equal to or a child directory of CefSettings.root_cache_path. If
// this value is empty then browsers will be created in "incognito mode" where
// in-memory caches are used for storage and no data is persisted to disk.
// HTML5 databases such as localStorage will only persist across sessions if a
// cache path is specified. Can be overridden for individual CefRequestContext
// instances via the CefRequestContextSettings.cache_path value. When using
// the Chrome runtime the "default" profile will be used if |cache_path| and
// |root_cache_path| have the same value.
///
cef_string_t cache_path;

///
// The root directory that all CefSettings.cache_path and
// CefRequestContextSettings.cache_path values must have in common. If this
// value is empty and CefSettings.cache_path is non-empty then it will
// default to the CefSettings.cache_path value. If this value is non-empty
// then it must be an absolute path. Failure to set this value correctly may
// result in the sandbox blocking read/write access to the cache_path
// directory.
///
cef_string_t root_cache_path;

实际测试之后,会缓存一些文件,但是不会缓存图片,如下图所示,但是翻页还会加载图片到内存,内存不断增加,到了一定值后还是会崩溃。并不会达到减少内存占用的问题。

 

 3.3SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);设置缓存到文件---实测无效

百度这个方法说是当程序内存占用过多,或者系统内存不够时,会将内存闲置不经常使用的缓存到本地文件。但是实测也无效;

 

3.4创建render渲染进程设置Application-cache参数------实测无效

CefBrowserSettings browser_settings;

browser_settings.application_cache = STATE_DISABLED;//禁用缓存
CefWindowInfo window_info;
window_info.SetAsPopup(NULL, "");
window_info.style &= ~WS_VISIBLE;

CefRefPtr<CefBrowser> b = CefBrowserHost::CreateBrowserSync(window_info, this, url, browser_settings, NULL,NULL);

 

3.5前端不去缓存上一页的图片---实测有效

前端去除采用懒加载方式去下载图片,翻一页不去缓存上一页的图片。

 4.总结

最终方案是让前端去修改,去除懒加载方式,不在缓存上一页的图片数据。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章