Skip to content

[🐛 Bug]: [dotnet] High memory usage after multiple PageSource access #16099

@dberubecoveo

Description

@dberubecoveo

Description

I noticed that when using the same ChromeDriver to retrieve multiple PageSource from different large pages, the memory consumption of my application was increasing drastically like there is some kind of memory leak.

After some digging, I found out that it seems to be coming from the usage of SharedArrayPool from System.Text.Json (which was introduced with Selenium 4 vs Selenium 3 that was using Newtonsoft, where I cannot reproduce the issue).

The SharedArrayPool is meant to improve memory usage by avoiding constant memory allocation and deallocation, but it looks like it just use a new chunk of memory instead of reusing it.

I was able to workaround this by overriding the HttpCommandExecutor handling of the Response and simply avoid deserializing the {"value":"<html....</html>"} to return the value content string directly. But that's not really a long term solution.

Image Image

Reproducible Code

var driverService= ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-dev-shm-usage");

using var webDriver = new ChromeDriver(driverService, options, TimeSpan.FromSeconds(60));

foreach (var url in GetUrls()) // GetUrls returns a list of large page URL.
{
    await webDriver.Navigate().GoToUrlAsync(url);
    var pageSource = webDriver.PageSource; // This is what is causing the high memory consumption.
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs-triagingA Selenium member will evaluate this soon!C-dotnet.NET Bindings

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions