Catching memory leaks with your test suite
Resource leaks are an unpleasant type of bug.
Little by little your program uses more memory, or more file descriptors, or some other limited resource.
Everything seems fine—until you run, and now your program is dead.
In many cases you can catch these sort of bugs in advance, by tweaking your test suite.
Or, after you’ve discovered such a bug, you can use your test suite to identify what is causing it.
In this article we’ll cover:
- An example of a memory leaks.
- When your test suite may be a good way to identify the causes of leaks.
- How to catch leaks using
pytest
. - Other types of leaks.
Memory leaks
Typically your program allocate some memory, and then eventually frees it.
In Python memory gets