Reputation: 2378
I am using selenium java to do web page scraping, basically the app creates a WebDriver and use it all the times for all pages required(every 1 or 2 seconds it will do a get() call for a new page and extract the related content).
I am using Firefox headless mode like this:
String driverPath = this.config.getString("browser.firefox.driverPath");
FirefoxBinary firefoxBinary = new FirefoxBinary();
if (useHeadlessMode) {
firefoxBinary.addCommandLineOptions("--headless");
}
System.setProperty("webdriver.gecko.driver", driverPath);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
webDriver = new FirefoxDriver(firefoxOptions);
I have realized that when the app running for 2 hours, it will use up to 8GB memory, and the get() call becomes extremely slow (could take around ~10 seconds).
My question is that do I miss any configuration when creating the WebDriver? Or any other solution to keep the memory usage in a low level, since I am considering to launch multiple (~100 WebDrivers) after deploying the app into the cloud.
The solution I am considering is that for a certain amount of operations, do driver.quit() for the current driver and initialize a new one. Does this sounds reasonable?
Upvotes: 0
Views: 1922
Reputation: 2760
First of all you need to understand that whenever you launch a browser using webdriver it create a temp profile in your "Temp" directory which consumes your memory.
To avoid that you can do 2 things :
Delete the data from "Temp" directory :
Create a Profile for your browser :
Type " firefox.exe –p" and press "ENTER" button Note: If it doesn't open you can try using full path enclosed in quotes.
A dialogue box will open named Firefox – choose user profile
Add this code in your program :
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("Your_Profile_Name");
// Initialize Firefox driver
WebDriver driver = new FirefoxDriver(myprofile);
Upvotes: 1