简体   繁体   中英

C# CefSharp Memory Management

I'm work with CefSharp Winforms (v57.0.0) in my C# Winform application (.NET 4.5.2), and it's been working well...

As I have to do several access to sites, the CEF Browser process consumes a lot of memory! I've already researched in the documentation and another forums about it but I didn't get a satisfactory answer... The "best" solution, until now, is restart the program.

Is there any way to "clear" the memory usage by CEF Browser process without restarting my program?

Follow the code of Cef initialization:

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;

And where it ends the program has the following code:

 Cef.Shutdown();

As amaitland mentioned, there was a bug in version 57 of CefSharp. Fortunately, version 63 was recently released that fixed this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM