如何在Selenium中释放和释放封闭的Chrome浏览器的记忆?
我编写了一个多线程自动化程序,该程序使用了很多使用Selenium和C#的Chrome浏览器(可以重复).
I wrote a multi-threading automation that uses a lot of multiple Chrome browsers (can be repeatedly) with Selenium and C#.
我试图使用包括"--incognito",-disable-application-cache"在内的所有内容,等待一秒钟后再打开另一个浏览器driver.Close()
,.Quit()
和.Dispose()
.这是代码示例:
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();
并建议我的客户端通过单击按钮执行以下代码来杀死所有"chromedriver.exe"进程:
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?
任何有效的解决方案如何在重新使用自动化之前防止客户端的高端PC重新启动以释放Chrome内存或冻结?还有其他选择吗?
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?
有命令杀死所有打开的浏览器窗口(chrome.exe)和与该浏览器窗口相关联的chrome驱动程序进程(chromedriver.exe)
There is command to kill all open browsers windows(chrome.exe) and chrome driver process associated with that browser window (chromedriver.exe)
杀死Chrome驱动程序进程
string strCmdText;
strCmdText= "TASKKILL /f /IM CHROMEDRIVER.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
杀死Chrome浏览器窗口
string strCmdText;
strCmdText= "TASKKILL /f /IM CHROME.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
有关更多信息,请参考 this 和此
希望这对您有帮助...
For more information refer this and this
hope this helps...