Selenium: How to Stop Geckodriver Process Impacting Pc Memory, Without Calling Driver.Quit()

Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

As per your question commenting out driver.quit() just Not to close firefox window after each run, because I just want to analyse what I have won't be a part of best practices.

For any detailed analysis we can create log entries and take snapshots.

While automating through Selenium as per the best practices you should invoke the quit() method within the tearDown() {}. Invoking quit() DELETEs the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint. Here is an example below :

1503397488598   webdriver::server   DEBUG   -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74 
1503397488607 geckodriver::marionette TRACE -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821 webdriver::server DEBUG -> GET /shutdown

So on invoking quit() method the Web Browser session and the WebDriver instance gets killed completely. Hence you don't have to incorporate any additional steps which will be an overhead.


Solution

Still if you want to execute kill the dangling WebDriver instances e.g. GeckoDriver.exe instances you can use either of the following code block to kill any of the dangling WebDriver instances :

  • Java Solution(Windows):

    import java.io.IOException;

    public class Kill_ChromeDriver_GeckoDriver_IEDriverserver
    {
    public static void main(String[] args) throws Exception
    {
    Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe /T");
    Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe /T");
    Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe /T");
    }
    }
  • Python Solution (Windows):

    import os
    os.system("taskkill /f /im geckodriver.exe /T")
    os.system("taskkill /f /im chromedriver.exe /T")
    os.system("taskkill /f /im IEDriverServer.exe /T")
  • Python Solution(Cross Platform):

    import os
    import psutil

    PROCNAME = "geckodriver" # or chromedriver or IEDriverServer
    for proc in psutil.process_iter():
    # check whether the process name matches
    if proc.name() == PROCNAME:
    proc.kill()

Python selenium - Multiprocessing - How to close browser for an allocated process?

Using Selenium to end the WebDriver and Web Browser session graciously you should invoke the quit() method within the tearDown() {}. Invoking quit() deletes the current browsing session through sending quit command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown endpoint. Here is the relevant log:

1503397488598   webdriver::server   DEBUG   -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74 
1503397488607 geckodriver::marionette TRACE -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821 webdriver::server DEBUG -> GET /shutdown

So on invoking quit() method the Web Browser session and the WebDriver instance gets killed completely.

References

You can find a couple of relevant detailed discussions in:

  • Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?
  • PhantomJS web driver stays in memory

How to quit all the Firefox processes which gets initiated through GeckoDriver and Selenium using Python

When automated tests are getting executed through Mozilla Firefox as you have observed that there are potentially half a dozen of Mozilla Firefox processes are running which can be observed through Windows Task Manager's Processes tab.

Multi-Process Firefox


Multi-Process Firefox

As per the article Multi-Process Firefox: everything you need to know to improve the browser's stability, performance and security Firefox had increased the number of content processes to four for the stable population in Firefox and is now multi-process Firefox which is also known as Electrolysis or e10S. Multi-process architecture helps in separating tasks into processes as Electrolysis functionality hosts, renders, or executes web related content in background child processes which communicate with the "parent" Firefox browser via various ipdl protocols. Additionally, multi-process Firefox moves NPAPI plugins, media playback and web content to child processes thus separating them from the browser's core.


Details

You can observe that several firefox.exe processes when you run a process manager, i.e. the Windows Task Manager which essentially implies that Electrolysis is enabled. Apparently Firefox should run just like before but ideally enabling multi-process functionality should improve the browser's performance and stability right away. However, you may however notice a higher than usual RAM usage. Mozilla confirmed that Firefox with Electrolysis will use about 20% more RAM. You may change how many processes Firefox uses for its multi-process functionality.


This usecase

In your first usecase when you invoke driver.quit() the parent and all the child Firefox processes and the parent Firefox process gets destroyed programatically.

You can find a detailed discussion in Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

In your second usecase, you have pressed Ctrl and c after 4 was printed, when KeyboardInterrupt occurs and your program is aborted and presumably GeckoDriver looses the control of the Mozilla Firefox browser process. However, still 1 process gets terminated through driver.quit() but the rest processes remains dangling.


Outro

You can find a relevant discussion in Many process of Google Chrome (32 bit)

When running the test(selenium) the browser window opens but disappears after a while

The @AfterEach annotation contains the method tearDown() where you have:

webDriver.quit();

So after each test, webDriver.quit(); is performed which not only closes the browser but ideally DELETEs the current webdriver session and the browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint.



References

You can find a couple of relevant detailed discussion in:

  • Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?
  • PhantomJS web driver stays in memory


Related Topics



Leave a reply



Submit