LeakWatch is a lightweight, easy-to-use Python library designed to help developers identify memory leaks and abnormal memory growth within Python applications.
Unlike traditional profilers that provide raw memory statistics, LeakWatch focuses on explaining:
- What is growing (which object types).
- Where it was created (source attribution / traceback).
- How fast it is growing (growth rate over time).
- Whether it is likely a memory leak (calculates a leak score).
- Snapshot Comparison: Capture and compare memory states dynamically.
- Context Monitoring: Monitor code blocks or decorate functions using
monitor. - Source Attribution: Resolve file and line tracebacks for allocated objects.
- Leak Scoring: Intelligent heuristic to identify the probability of a leak.
- Report Export: Generate reports in JSON, Markdown, or beautiful dark-themed HTML.
- Command-Line Interface: Run your Python scripts under a monitor or export reports.
Install using pip:
pip install leakwatch-pyfrom leakwatch import watch
watch()
# Your application code runs...from leakwatch import snapshot
snapshot("before")
# ... your code ...
snapshot("after")from leakwatch import monitor
with monitor():
run_application()from leakwatch import report
report(format='html', path='report.html')
report(format='json', path='report.json')
report(format='markdown', path='report.md')Monitor a script:
leakwatch run python app.pyExport a report:
leakwatch report --html
leakwatch report --json
leakwatch report --markdownLeakWatch uses Python's built-in tracemalloc and gc modules to:
- Capture snapshots of memory allocations and object counts at different points in time.
- Analyze growth patterns by comparing snapshots to identify which object types are increasing.
- Compute a leak score using heuristics such as growth percentage, monotonic increase detection, and object classification (built-in vs custom types).
- Attribute sources by resolving
tracemalloctracebacks to find where leaking objects were allocated.
MIT License - see LICENSE for details.
Vicky — TrixSec