Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium using too much RAM with Firefox

I am using selenium with Firefox to automate some tasks on Instagram. It basically goes back and forth between user profiles and notifications page and does tasks based on what it finds.

It has one infinite loop that makes sure that the task keeps on going. I have sleep() function every few steps but the memory usage keeps increasing. I have something like this in Python:

while(True):
    expected_conditions()
    ...doTask()
    driver.back()
    expected_conditions()
    ...doAnotherTask()
    driver.forward()
    expected_conditions()

I never close the driver because that will slow down the program by a lot as it has a lot of queries to process. Is there any way to keep the memory usage from increasing overtime without closing or quitting the driver?

EDIT: Added explicit conditions but that did not help either. I am using headless mode of Firefox.

like image 856
Naeem Khan Avatar asked Mar 09 '19 00:03

Naeem Khan


People also ask

Why is Firefox taking up so much RAM?

If Firefox is using too much memory, it might be clogged by extensions or unnecessary settings. Another reason for Firefox consuming too much memory is intrusive content or certain scrips. So to cut on Firefox RAM usage, you will need to check and tweak several browser settings.

Does Firefox use more RAM than Chrome?

Coming in at number 3 is Mozilla's popular and privacy-minded browser, Firefox. And no, despite the browser's reputation for efficiency, it can take up almost just as much RAM as Chrome. When tested with 10 tabs open, Firefox occupied about 960MBs of memory, which is only slightly less than Chrome.

Why is Firefox power usage so high?

High CPU usage in the Firefox browser is mainly due to the parsing of heavy website elements and background processes. It can be fixed by tweaking browser configuration and avoid using a large number of third-party add-ons running in the background.


1 Answers

Well, This the serious problem I've been going through for some days. But I have found the solution. You can add some flags to optimize your memory usage.

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--no-sandbox')
options.add_argument('--disable-application-cache')
options.add_argument('--disable-gpu')
options.add_argument("--disable-dev-shm-usage")

These are the flags I added. Before I added the flags RAM usage kept increasing after it crosses 4GB (8GB my machine) my machine stuck. after I added these flags memory usage didn't cross 500MB. And as DebanjanB answers, if you running for loop or while loop tries to put some seconds sleep after each execution it will give some time to kill the unused thread.

like image 190
Darkknight Avatar answered Oct 13 '22 05:10

Darkknight