0

Based on CefSharp.MinimalExample i created similar issue to that im having in my app. I creating many browsers in background, every of them have certain tasks, and then they should close and dispose. But i noticed every offscreen browser i create, adds to used memory around 4MB, and it's something called BitmapBuffer, why it's not disposing? I am doing something wrong?

Code:

public static int Main(string[] args)
    {
        CefSharpSettings.SubprocessExitIfParentProcessClosed = true;
        CefSharpSettings.WcfEnabled = false;

        var settings = new CefSettings()
        {
            //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
            CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache"),
        };

        settings.CefCommandLineArgs.Add("disable-gpu-compositing", string.Empty);

        Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

        for (int i = 0; i < 150; i++)
        {
            Thread.Sleep(1000);
            Task.Run(CreateAndDispose);
        }

        //GC.Collect(); does not change anything

        Console.ReadKey();

        return 0;
    }

    public static async Task CreateAndDispose()
    {

        var browser = new ChromiumWebBrowser("https://www.google.com/");


        browser.GetBrowser().CloseBrowser(true);
        browser.RenderHandler = null
        browser.Dispose();
        browser = null;


    }

Memory view of an exmaple

EDIT: Instead of 10 i created 150 instances of offscren browser, because i thought 80 mb of used memory is too low for GC to care, but it didn't changed anything and now it's 800mb of used memory with same problem.

I launched example 3-4 times, and waited 10 to 20 minutes for memory to free with same result.

Memory view and it change after some time

Memory view after 16 minutes

Crazy
  • 1
  • 1
  • 1
    Is the memory snapshot of your application or the example you've posted above? When is the snapshot taken? Calling `ChromiumWebBrowser.Dispose` will close the browser and release a number of references. `CEF` will actually close the underlying browser on a different thread which will take a little bit of time. You can call `browser.RenderHandler = null` before calling `Dispose`. – amaitland Jun 02 '22 at 23:30
  • @amaitland edited question, still same issue. I added your line (browser.RenderHandler =null) with no result at all. – Crazy Jun 03 '22 at 07:04
  • Please answer my first two questions. – amaitland Jun 03 '22 at 07:15
  • @amaitland 1)Of example i created 2)It's visible on second and third photo when snapshots are taken. Also i'm using the newest version of CefSharp – Crazy Jun 03 '22 at 09:34
  • It's possible there is a memory leak. Does your profiler say what is holding the `RenderHandler` reference? A quick look and you'd need to actually call `RenderHandler.Dispose` before setting the reference to null to release the reference. If you can provide some insight into what is holding the `RenderHandler` reference that would be very helpful thanks. I don't have a license for a paid memory profiler. – amaitland Jun 08 '22 at 00:28
  • @amaitland screenshots of renderhandler and webbrowser in memory profiler with more details: https://i.imgur.com/4I9TuVl.png https://i.imgur.com/sJsr85W.png https://i.imgur.com/tshzHZC.png https://i.imgur.com/6jTEv8Q.png https://i.imgur.com/v6I1uLj.png – Crazy Jun 09 '22 at 07:50
  • Unfortunately resolution is too low for them to be readable in my tablet. What's holding the reference? – amaitland Jun 09 '22 at 08:22
  • @amaitland RenderHandler reference is keeped by ChromiumWebBrowser, so nothing special here. But ChromiumWebBrowser is referenced by , ManagedCefBrowserAdapter and RenderHandler itself – Crazy Jun 09 '22 at 09:36
  • What profiler are you using? Commit https://github.com/cefsharp/CefSharp/commit/93dcfacef2517f9663d7d8ce5a7699f69f33f2c4 will explicitly dispose of the renderhandler and will be in the next major release. The managedcefbrowseradapter references should already be released https://github.com/cefsharp/CefSharp/blob/master/CefSharp.OffScreen/ChromiumWebBrowser.cs#L296 https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.h#L122 – amaitland Jun 09 '22 at 20:23
  • If you'd like to debug further and submit a pull request that would be most welcome, otherwise someone will need to provide me with a paid license for a memory profiler. – amaitland Jun 09 '22 at 20:24

0 Answers0