20.1. Debugging Tools¶
First create your toolbox, in this one we have:
Debug version of Python - great for finding out more detail of your Python code as it executes.
Valgrind - the goto tool for memory leaks. It is a little tricky to get working but should be in every developers toolbox.
OS memory monitioring - this is a quick and simple way of identifying whether memory leaks are happening or not. An example is given below: A Simple Memory Monitor
20.1.1. Build a Debug Version of Python¶
There are a large combination of debug builds of Python that you can create and each one will give you extra information when you either:
Invoke Python with a command line option.
Example: a
Py_DEBUGbuild invoking Python withpython -X showrefcount
Set an environment variable.
Example: a
Py_DEBUGbuild invoking Python withPYTHONMALLOCSTATS=1 python
Additional functions that are added to the
sysmodule that can give useful information.Example: a
Py_DEBUGbuild an callingsys.getobjects(...).
See here Building and Using a Debug Version of Python for instructions on how to do this.
20.1.2. Valgrind¶
See here Building Python for Valgrind for instructions on how to build Valgrind.
See here Using Valgrind for instructions on how to use Valgrind.
Here Finding Where the Leak is With Valgrind is an example of finding a leak with Valgrind.
20.1.3. A Simple Memory Monitor¶
A useful technique is to monitor the memory usage of a Python program.
Here is a simple process memory monitor using the psutil library.
See the Memory Leaks chapter for a more comprehensive approach, in particular
pymemtrace.
1import sys
2import time
3
4import psutil
5
6def memMon(pid, freq=1.0):
7 proc = psutil.Process(pid)
8 print(proc.memory_info_ex())
9 prev_mem = None
10 while True:
11 try:
12 mem = proc.memory_info().rss / 1e6
13 if prev_mem is None:
14 print('{:10.3f} [Mb]'.format(mem))
15 else:
16 print('{:10.3f} [Mb] {:+10.3f} [Mb]'.format(mem, mem - prev_mem))
17 prev_mem = mem
18 time.sleep(freq)
19 except KeyboardInterrupt:
20 try:
21 input(' Pausing memMon, <cr> to continue, ^C to end...')
22 except KeyboardInterrupt:
23 print('\n')
24 return
25
26if __name__ == '__main__':
27 if len(sys.argv) < 2:
28 print('Usage: python pidmon.py <PID>')
29 sys.exit(1)
30 pid = int(sys.argv[1])
31 memMon(pid)
32 sys.exit(0)
Lets test it. In one shell fire up Python and find its PID:
>>> import os
>>> os.getpid()
13360
In a second shell fire up pidmon.py with this PID:
$ python3 pidmon.py 13360
pextmem(rss=7364608, vms=2526482432, pfaults=9793536, pageins=24576)
7.365 [Mb]
7.365 [Mb] +0.000 [Mb]
7.365 [Mb] +0.000 [Mb]
...
Pause pidmon.py with Ctrl-C:
^C Pausing memMon, <cr> to continue, ^C to end...
Go back to the first shell and create a large string (1Gb):
>>> s = ' ' * 1024**3
In the second shell continue pidmon.py with <cr> and we see the memory usage:
1077.932 [Mb] +1070.567 [Mb]
1077.932 [Mb] +0.000 [Mb]
1077.932 [Mb] +0.000 [Mb]
...
Go back to the first shell and delete the string:
>>> del s
In the second shell we see the memory usage drop:
1077.953 [Mb] +0.020 [Mb]
1077.953 [Mb] +0.000 [Mb]
272.679 [Mb] -805.274 [Mb]
4.243 [Mb] -268.435 [Mb]
4.243 [Mb] +0.000 [Mb]
...
In the second shell halt pidmon with two Ctrl-C commands:
^C Pausing memMon, <cr> to continue, ^C to end...^C
So we can observe the total memory usage of another process simply and cheaply. This is often the first test to do when examining processes for memory leaks.