Reputation: 4479
I wrote a multi-threading automation that uses a lot of multiple Chrome browsers (can be repeatedly) with Selenium and C#.
I tried to use everything including "--incognito", "--disable-application-cache", waiting a second before opening another browser, driver.Close()
, .Quit()
and .Dispose()
. Here's the code sample:
IWebDriver[] drivers = new IWebDriver[AUTOMATION_NUM];
int QUEUE_BROWSERS_TO_OPEN = 0;
Thread mainThread = new Thread(() =>
{
for (int i = 0; i < AUTOMATION_NUM && !EXIT; i++)
{
...
Thread driverThread = new Thread(() => {
...
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--incognito", "--disable-application-cache"); // headless or non-headless
IWebDriver driver = drivers[instance];
driver = new ChromeDriver(chromeService, chromeOptions, TIMEOUT_FROM);
...
++QUEUE_BROWSERS_TO_OPEN;
...
driver.Close();
driver.Quit();
driver.Dispose();
});
_machineThreads.Add(driverThread);
driverThread.IsBackground = true;
driverThread.Start();
while (i < AUTOMATION_NUM && !EXIT)
{
Thread.Sleep(5000);
if (QUEUE_BROWSERS_TO_OPEN > 0 && BROWSERS_OPEN < BROWSER_NUM)
--QUEUE_BROWSERS_TO_OPEN;
}
}
Thread endThread = new Thread(() =>
{
foreach (Thread machineThread in _machineThreads)
machineThread.Join();
...
});
endThread.Start();
});
mainThread.Start();
And have suggested my client to kill all "chromedriver.exe" processes by clicking a button to execute this code:
Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver");
foreach (var chromeDriverProcess in chromeDriverProcesses)
chromeDriverProcess.Kill();
But my client is still saying that the closed and finished automated browsers' memories are still there?
Any working solution how can I prevent my client's high-end PC from restarting to free up Chrome memory or from being frozen before using the automation again? Any other alternative?
Upvotes: 0
Views: 586
Reputation: 2813
There is command to kill all open browsers windows(chrome.exe) and chrome driver process associated with that browser window (chromedriver.exe)
To kill chrome driver process
string strCmdText;
strCmdText= "TASKKILL /f /IM CHROMEDRIVER.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
To kill chrome browser window
string strCmdText;
strCmdText= "TASKKILL /f /IM CHROME.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
For more information refer this and this
hope this helps...
Upvotes: 1