High RAM usage with SeleniumBase - how to properly free resources between iterations? #4159
-
|
I'm processing several thousand URLs using SeleniumBase with undetected-chromedriver mode, but I'm experiencing significant memory accumulation over time. Even though I'm calling driver.close() and driver.quit() in the finally block, CPU* usage keeps growing with each iteration. Questions:
Any guidance on preventing memory leaks when processing large numbers of URLs would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
The Use the from seleniumbase import SB
with SB(uc=True) as sb:
sb.open(URL)
# ...You can also try the Pure CDP Mode, which has less memory usage because it doesn't use Selenium/WebDriver at all: from seleniumbase import sb_cdp
sb = sb_cdp.Chrome(url)
sb.open(URL)You can use import objgraph
from seleniumbase import sb_cdp
print("\nBefore opening the browser:")
objgraph.show_growth(limit=50)
url = "https://seleniumbase.io/demo_page"
sb = sb_cdp.Chrome(url)
for i in range(10):
sb.open(url)
sb.sleep(0.1)
print("\nAfter browser is open:")
objgraph.show_growth(limit=50)
sb.driver.stop()
print("\nAfter closing the browser:")
objgraph.show_growth(limit=50)Final Output: (After closing the browser) (The |
Beta Was this translation helpful? Give feedback.
The
Driver()isn't a context manager format, so you can't guarantee all resources being freed until the Python program terminates.Use the
SB()format to guarantee resources being freed at the end of thewithblock:You can also try the Pure CDP Mode, which has less memory usage because it doesn't use Selenium/WebDriver at all:
You can use
objgraphto profile the memory usage. Here's an example with the Pure CDP (sb_cdp) format: