I am using selenium in c#, but every time I run var html=driver.PageSource;
in the code below, the memory used increases and quickly fills up.
Within an hour, 4GB of memory is used and OutOfMemoryException occurs.
I put driver.Quit();
in front of driver.Close()
and the following code is written in a class, but I try to run GC.Collect()
after null in that class, but it doesn't release any memory.
Note: The version of Selenium.Webdriver is 3.141.0
The version of Selenium.WebDriver.Chromedriver is 84.04147.3001.
I'm thinking about forcing the target application to restart, but if anyone knows how to free up the memory used in selenium, I'd appreciate it if you could let me know.
driver = new ChromeDriver (driverService, options, TimeSpan.FromSeconds (60));
driver.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(60);
driver.Navigate().GoToUrl(this.Url);
ReadOnlyCollection<IWebElement>elements=driver.FindElements(By.XPath(@"//a[@href!=' and normalize-space(.)!=']);
varhtml = driver.PageSource;
elements = null;
driver.Quit();
// Discard the chromedriver process
KillProcessAndChildren("chromedriver.exe");
driver = null;
return html;
When you ask questions, you should provide detailed information about your environment.
In some cases, minor differences between OS, IDE, runtime, and virtual machine versions are important.
What exactly do you do in KillProcessAndChildren()
?
Specifying "chromedriver.exe"
is probably a Windows environment, but Selenium is a cross-platform automation solution, so you should think something is wrong when you write the code for the Windows environment unconditionally.If you are forcing the process to terminate without permission, it may have caused the condition to go wrong.
The ChromeDriver
class is derived from the ChromeDriver
, the ChromeDriver
class is derived from the RemoteWebDriver
, and the class implements the
IWebDriver
interface.
You should also use the try-finally
statement so that the Quit()
method is always called if an exception occurs along the way.Currently, inside the RemoteWebDriver.Quit()
, you simply invoke the IDisposable.Dispose()
method, so you may use the using
statement, but it depends on the RemoteWebDriver
statement.
PageSource
HTML strings are stored in DB.If the DB remains mapped to memory (so-called non-intentional object retention), it leaks.In order to determine if the memory usage problem is really due to Selenium, from the point of view of the control experiment, you should also try what happens if DB is not saved.
© 2024 OneMinuteCode. All rights reserved.