C#CefSharp内存管理

时间:2018-01-23 09:48:37

标签: c# .net memory cefsharp

我在C#Winform应用程序(.NET 4.5.2)中使用CefSharp Winforms(v57.0.0),并且它运行良好......

由于我必须对网站进行多次访问,因此CEF浏览器进程会消耗大量内存!我已经在文档和其他论坛上研究了它,但我没有得到满意的答案......最好的"解决方案,直到现在,重启程序。

有没有办法清除" CEF浏览器进程的内存使用情况,而不重新启动我的程序?

遵循Cef初始化代码:

CefSettings settings = new CefSettings();
settings.CachePath = Program.CACHE_PATH; // cache path
settings.IgnoreCertificateErrors = true;
settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
settings.PersistSessionCookies = false;
// Initialize cef with the provided settings
bool initialized = false;
string cefError = string.Empty;
try
{
   Thread.Sleep(1000);
   initialized = Cef.Initialize(settings, true, null);
}
catch (Exception ex)
{
   cefError = ex.InnerException == null ? ex.Message : ex.InnerException.InnerException == null ? ex.InnerException.Message : ex.InnerException.InnerException.Message;
}

if (!initialized)
{
  // ERROR! => FINISH PROGRAM
  // ...
  return;
}

// Cookies path
Cef.GetGlobalCookieManager().SetStoragePath(Program.COOKIES_PATH, false);

// Create a browser component
webBrowser = new ChromiumWebBrowser("about:blank");
webBrowser.Name = "WebBrowser";
webBrowser.TabIndex = 0;
webBrowser.BrowserSettings.ApplicationCache = CefState.Disabled;
webBrowser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
webBrowser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
webBrowser.BrowserSettings.ImageLoading = CefState.Disabled;
webBrowser.BrowserSettings.Javascript = CefState.Enabled;
webBrowser.BrowserSettings.WebSecurity = CefState.Disabled;
webBrowser.KeyboardHandler = new BrowserChrome.KeyboardHandler(); // prevent keyboard
webBrowser.JsDialogHandler = new BrowserChrome.JsHandler(); // prevent alerts

// Add to tab (tabBrowser is a TabPage)
tabBrowser.Controls.Add(webBrowser);

webBrowser.Dock = DockStyle.Fill;

它结束的地方程序有以下代码:

 Cef.Shutdown();

1 个答案:

答案 0 :(得分:0)

正如amaitland所提到的,CefSharp的第57版中存在一个错误。幸运的是,最近发布了版本63来解决这个问题。