Digiaru

Digiaru started this conversation 1 week ago.

Subprocess.Popen causing ghost memory leaks on Windows

I run Python scripts using subprocess.Popen repeatedly in a loop. Over time, system memory increases, even after processes complete. The Python process itself doesn’t grow—but system memory remains tied up. What’s going wrong?

Kar

Posted 1 week ago

On Windows, terminated child processes created via subprocess.Popen may leave behind Page Table Entries (PTEs) that aren’t freed, causing system memory usage to spike. The leak isn't in Python memory, but at the OS level Reddit+1Reddit+1. 🛠️ How to Fix • Reuse processes via persistent pools instead of spawning new ones repeatedly. • Always call .communicate() and ensure .stdout.close(), followed by explicit .wait() or .kill() to clean up resources. • Consider using job queues or native APIs for frequent shell commands to avoid OS-level leaks.