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.總結

最終方案是讓前端去修改,去除懶加載方式,不在緩存上一頁的圖片數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM