-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Description
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.


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.
...
}