I have a long-running script which, if let to run long enough, will consume all the memory on my system.
Without going into details about the script, I have two questions:
- Are there any "Best Practices" to follow, which will help prevent leaks from occurring?
- What techniques are there to debug memory leaks in Python?
To detect and locate memory leaks for long running processes, e.g. in production environments, you can now use stackimpact. It uses tracemalloc underneath. More info in this post.
This is by no means exhaustive advice. But number one thing to keep in mind when writing with the thought of avoiding future memory leaks (loops) is to make sure that anything which accepts a reference to a call-back, should store that call-back as a weak reference.
Not sure about "Best Practices" for memory leaks in python, but python should clear it's own memory by it's garbage collector. So mainly I would start by checking for circular list of some short, since they won't be picked up by the garbage collector.