Handling Memory Leaks in Puppeteer on WSL2

I wrote a puppeteer scraper running on WSL2 with proxies. Initially, it worked well, but over time, it started crashing the Linux instance. After frequent wsl --shutdown commands, I discovered the issue: /tmp was filling up, causing a memory leak.

Tried to mitigate this with --no-sandbox flag and ensuring browser.close() and await browser.close() were called didn't help. Since /tmp is a tmpfs and uses RAM, I manually cleared it with rm -rf /tmp/* to prevent crashes. This workaround was effective enough to me, but calling manually was not ideal.

Then, I did a command in bash that monitors the size of the /tmp directory and clears it if it exceeds a certain threshold.

watch -n 1 'du -sh /tmp | awk '\''{ print; if ($1 ~ /[0-9.]+G/ && $1+0 > 7) system("rm -rf /tmp/*") }'\'
  1. watch -n 1: Runs the specified command every second using the watch utility.
  2. du -sh /tmp: Summarizes the size of the /tmp directory in a human-readable format.
  3. awk '\''{ print; if ($1 ~ /[0-9.]+G/ && $1+0 > 7) system("rm -rf /tmp/*") }'\'': Prints the size of /tmp. If the size is in gigabytes and exceeds 7GB, it executes rm -rf /tmp/* to clear the directory.

Aaaand, that's it!