Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 1 | :mod:`tracemalloc` --- Trace memory allocations |
| 2 | =============================================== |
| 3 | |
| 4 | .. module:: tracemalloc |
| 5 | :synopsis: Trace memory allocations. |
| 6 | |
R David Murray | 6d5cb07 | 2013-12-20 14:48:50 -0500 | [diff] [blame] | 7 | .. versionadded:: 3.4 |
| 8 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 9 | **Source code:** :source:`Lib/tracemalloc.py` |
| 10 | |
| 11 | -------------- |
| 12 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 13 | The tracemalloc module is a debug tool to trace memory blocks allocated by |
| 14 | Python. It provides the following information: |
| 15 | |
| 16 | * Traceback where an object was allocated |
| 17 | * Statistics on allocated memory blocks per filename and per line number: |
| 18 | total size, number and average size of allocated memory blocks |
| 19 | * Compute the differences between two snapshots to detect memory leaks |
| 20 | |
| 21 | To trace most memory blocks allocated by Python, the module should be started |
| 22 | as early as possible by setting the :envvar:`PYTHONTRACEMALLOC` environment |
| 23 | variable to ``1``, or by using :option:`-X` ``tracemalloc`` command line |
| 24 | option. The :func:`tracemalloc.start` function can be called at runtime to |
| 25 | start tracing Python memory allocations. |
| 26 | |
| 27 | By default, a trace of an allocated memory block only stores the most recent |
| 28 | frame (1 frame). To store 25 frames at startup: set the |
| 29 | :envvar:`PYTHONTRACEMALLOC` environment variable to ``25``, or use the |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 30 | :option:`-X` ``tracemalloc=25`` command line option. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 31 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 32 | |
| 33 | Examples |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 34 | -------- |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 35 | |
| 36 | Display the top 10 |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 37 | ^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 38 | |
| 39 | Display the 10 files allocating the most memory:: |
| 40 | |
| 41 | import tracemalloc |
| 42 | |
| 43 | tracemalloc.start() |
| 44 | |
| 45 | # ... run your application ... |
| 46 | |
| 47 | snapshot = tracemalloc.take_snapshot() |
| 48 | top_stats = snapshot.statistics('lineno') |
| 49 | |
| 50 | print("[ Top 10 ]") |
| 51 | for stat in top_stats[:10]: |
| 52 | print(stat) |
| 53 | |
| 54 | |
| 55 | Example of output of the Python test suite:: |
| 56 | |
| 57 | [ Top 10 ] |
| 58 | <frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B |
| 59 | <frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 B |
| 60 | /usr/lib/python3.4/collections/__init__.py:368: size=244 KiB, count=2315, average=108 B |
| 61 | /usr/lib/python3.4/unittest/case.py:381: size=185 KiB, count=779, average=243 B |
| 62 | /usr/lib/python3.4/unittest/case.py:402: size=154 KiB, count=378, average=416 B |
| 63 | /usr/lib/python3.4/abc.py:133: size=88.7 KiB, count=347, average=262 B |
| 64 | <frozen importlib._bootstrap>:1446: size=70.4 KiB, count=911, average=79 B |
| 65 | <frozen importlib._bootstrap>:1454: size=52.0 KiB, count=25, average=2131 B |
| 66 | <string>:5: size=49.7 KiB, count=148, average=344 B |
| 67 | /usr/lib/python3.4/sysconfig.py:411: size=48.0 KiB, count=1, average=48.0 KiB |
| 68 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 69 | We can see that Python loaded ``4855 KiB`` data (bytecode and constants) from |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 70 | modules and that the :mod:`collections` module allocated ``244 KiB`` to build |
| 71 | :class:`~collections.namedtuple` types. |
| 72 | |
| 73 | See :meth:`Snapshot.statistics` for more options. |
| 74 | |
| 75 | |
| 76 | Compute differences |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 77 | ^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 78 | |
| 79 | Take two snapshots and display the differences:: |
| 80 | |
| 81 | import tracemalloc |
| 82 | tracemalloc.start() |
| 83 | # ... start your application ... |
| 84 | |
| 85 | snapshot1 = tracemalloc.take_snapshot() |
| 86 | # ... call the function leaking memory ... |
| 87 | snapshot2 = tracemalloc.take_snapshot() |
| 88 | |
| 89 | top_stats = snapshot2.compare_to(snapshot1, 'lineno') |
| 90 | |
| 91 | print("[ Top 10 differences ]") |
| 92 | for stat in top_stats[:10]: |
| 93 | print(stat) |
| 94 | |
| 95 | Example of output before/after running some tests of the Python test suite:: |
| 96 | |
| 97 | [ Top 10 differences ] |
| 98 | <frozen importlib._bootstrap>:716: size=8173 KiB (+4428 KiB), count=71332 (+39369), average=117 B |
| 99 | /usr/lib/python3.4/linecache.py:127: size=940 KiB (+940 KiB), count=8106 (+8106), average=119 B |
| 100 | /usr/lib/python3.4/unittest/case.py:571: size=298 KiB (+298 KiB), count=589 (+589), average=519 B |
| 101 | <frozen importlib._bootstrap>:284: size=1005 KiB (+166 KiB), count=7423 (+1526), average=139 B |
| 102 | /usr/lib/python3.4/mimetypes.py:217: size=112 KiB (+112 KiB), count=1334 (+1334), average=86 B |
| 103 | /usr/lib/python3.4/http/server.py:848: size=96.0 KiB (+96.0 KiB), count=1 (+1), average=96.0 KiB |
| 104 | /usr/lib/python3.4/inspect.py:1465: size=83.5 KiB (+83.5 KiB), count=109 (+109), average=784 B |
| 105 | /usr/lib/python3.4/unittest/mock.py:491: size=77.7 KiB (+77.7 KiB), count=143 (+143), average=557 B |
| 106 | /usr/lib/python3.4/urllib/parse.py:476: size=71.8 KiB (+71.8 KiB), count=969 (+969), average=76 B |
| 107 | /usr/lib/python3.4/contextlib.py:38: size=67.2 KiB (+67.2 KiB), count=126 (+126), average=546 B |
| 108 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 109 | We can see that Python has loaded ``8173 KiB`` of module data (bytecode and |
| 110 | constants), and that this is ``4428 KiB`` more than had been loaded before the |
Victor Stinner | 00773df | 2013-11-26 00:40:10 +0100 | [diff] [blame] | 111 | tests, when the previous snapshot was taken. Similarly, the :mod:`linecache` |
| 112 | module has cached ``940 KiB`` of Python source code to format tracebacks, all |
| 113 | of it since the previous snapshot. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 114 | |
| 115 | If the system has little free memory, snapshots can be written on disk using |
| 116 | the :meth:`Snapshot.dump` method to analyze the snapshot offline. Then use the |
| 117 | :meth:`Snapshot.load` method reload the snapshot. |
| 118 | |
| 119 | |
| 120 | Get the traceback of a memory block |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 121 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 122 | |
| 123 | Code to display the traceback of the biggest memory block:: |
| 124 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 125 | import tracemalloc |
| 126 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 127 | # Store 25 frames |
| 128 | tracemalloc.start(25) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 129 | |
| 130 | # ... run your application ... |
| 131 | |
| 132 | snapshot = tracemalloc.take_snapshot() |
| 133 | top_stats = snapshot.statistics('traceback') |
| 134 | |
| 135 | # pick the biggest memory block |
| 136 | stat = top_stats[0] |
| 137 | print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024)) |
Victor Stinner | 23f628d | 2014-02-16 23:53:38 +0100 | [diff] [blame] | 138 | for line in stat.traceback.format(): |
| 139 | print(line) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 140 | |
| 141 | Example of output of the Python test suite (traceback limited to 25 frames):: |
| 142 | |
| 143 | 903 memory blocks: 870.1 KiB |
| 144 | File "<frozen importlib._bootstrap>", line 716 |
| 145 | File "<frozen importlib._bootstrap>", line 1036 |
| 146 | File "<frozen importlib._bootstrap>", line 934 |
| 147 | File "<frozen importlib._bootstrap>", line 1068 |
| 148 | File "<frozen importlib._bootstrap>", line 619 |
| 149 | File "<frozen importlib._bootstrap>", line 1581 |
| 150 | File "<frozen importlib._bootstrap>", line 1614 |
| 151 | File "/usr/lib/python3.4/doctest.py", line 101 |
| 152 | import pdb |
| 153 | File "<frozen importlib._bootstrap>", line 284 |
| 154 | File "<frozen importlib._bootstrap>", line 938 |
| 155 | File "<frozen importlib._bootstrap>", line 1068 |
| 156 | File "<frozen importlib._bootstrap>", line 619 |
| 157 | File "<frozen importlib._bootstrap>", line 1581 |
| 158 | File "<frozen importlib._bootstrap>", line 1614 |
| 159 | File "/usr/lib/python3.4/test/support/__init__.py", line 1728 |
| 160 | import doctest |
| 161 | File "/usr/lib/python3.4/test/test_pickletools.py", line 21 |
| 162 | support.run_doctest(pickletools) |
| 163 | File "/usr/lib/python3.4/test/regrtest.py", line 1276 |
| 164 | test_runner() |
| 165 | File "/usr/lib/python3.4/test/regrtest.py", line 976 |
| 166 | display_failure=not verbose) |
| 167 | File "/usr/lib/python3.4/test/regrtest.py", line 761 |
| 168 | match_tests=ns.match_tests) |
| 169 | File "/usr/lib/python3.4/test/regrtest.py", line 1563 |
| 170 | main() |
| 171 | File "/usr/lib/python3.4/test/__main__.py", line 3 |
| 172 | regrtest.main_in_temp_cwd() |
| 173 | File "/usr/lib/python3.4/runpy.py", line 73 |
| 174 | exec(code, run_globals) |
| 175 | File "/usr/lib/python3.4/runpy.py", line 160 |
| 176 | "__main__", fname, loader, pkg_name) |
| 177 | |
Victor Stinner | 00773df | 2013-11-26 00:40:10 +0100 | [diff] [blame] | 178 | We can see that the most memory was allocated in the :mod:`importlib` module to |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 179 | load data (bytecode and constants) from modules: ``870.1 KiB``. The traceback is |
Victor Stinner | 00773df | 2013-11-26 00:40:10 +0100 | [diff] [blame] | 180 | where the :mod:`importlib` loaded data most recently: on the ``import pdb`` |
| 181 | line of the :mod:`doctest` module. The traceback may change if a new module is |
| 182 | loaded. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 183 | |
| 184 | |
| 185 | Pretty top |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 186 | ^^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 187 | |
| 188 | Code to display the 10 lines allocating the most memory with a pretty output, |
| 189 | ignoring ``<frozen importlib._bootstrap>`` and ``<unknown>`` files:: |
| 190 | |
Victor Stinner | 637d2e9 | 2014-03-11 08:12:48 +0100 | [diff] [blame] | 191 | import linecache |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 192 | import os |
| 193 | import tracemalloc |
| 194 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 195 | def display_top(snapshot, key_type='lineno', limit=10): |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 196 | snapshot = snapshot.filter_traces(( |
| 197 | tracemalloc.Filter(False, "<frozen importlib._bootstrap>"), |
| 198 | tracemalloc.Filter(False, "<unknown>"), |
| 199 | )) |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 200 | top_stats = snapshot.statistics(key_type) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 201 | |
| 202 | print("Top %s lines" % limit) |
| 203 | for index, stat in enumerate(top_stats[:limit], 1): |
| 204 | frame = stat.traceback[0] |
| 205 | # replace "/path/to/module/file.py" with "module/file.py" |
| 206 | filename = os.sep.join(frame.filename.split(os.sep)[-2:]) |
| 207 | print("#%s: %s:%s: %.1f KiB" |
Victor Stinner | f70200e | 2014-03-03 11:57:57 +0100 | [diff] [blame] | 208 | % (index, filename, frame.lineno, stat.size / 1024)) |
Victor Stinner | 637d2e9 | 2014-03-11 08:12:48 +0100 | [diff] [blame] | 209 | line = linecache.getline(frame.filename, frame.lineno).strip() |
| 210 | if line: |
| 211 | print(' %s' % line) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 212 | |
| 213 | other = top_stats[limit:] |
| 214 | if other: |
| 215 | size = sum(stat.size for stat in other) |
| 216 | print("%s other: %.1f KiB" % (len(other), size / 1024)) |
| 217 | total = sum(stat.size for stat in top_stats) |
| 218 | print("Total allocated size: %.1f KiB" % (total / 1024)) |
| 219 | |
| 220 | tracemalloc.start() |
| 221 | |
| 222 | # ... run your application ... |
| 223 | |
| 224 | snapshot = tracemalloc.take_snapshot() |
Victor Stinner | f70200e | 2014-03-03 11:57:57 +0100 | [diff] [blame] | 225 | display_top(snapshot) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 226 | |
| 227 | Example of output of the Python test suite:: |
| 228 | |
Victor Stinner | 637d2e9 | 2014-03-11 08:12:48 +0100 | [diff] [blame] | 229 | Top 10 lines |
| 230 | #1: Lib/base64.py:414: 419.8 KiB |
| 231 | _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars] |
| 232 | #2: Lib/base64.py:306: 419.8 KiB |
| 233 | _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars] |
| 234 | #3: collections/__init__.py:368: 293.6 KiB |
| 235 | exec(class_definition, namespace) |
| 236 | #4: Lib/abc.py:133: 115.2 KiB |
| 237 | cls = super().__new__(mcls, name, bases, namespace) |
| 238 | #5: unittest/case.py:574: 103.1 KiB |
| 239 | testMethod() |
| 240 | #6: Lib/linecache.py:127: 95.4 KiB |
| 241 | lines = fp.readlines() |
| 242 | #7: urllib/parse.py:476: 71.8 KiB |
| 243 | for a in _hexdig for b in _hexdig} |
| 244 | #8: <string>:5: 62.0 KiB |
| 245 | #9: Lib/_weakrefset.py:37: 60.0 KiB |
| 246 | self.data = set() |
| 247 | #10: Lib/base64.py:142: 59.8 KiB |
| 248 | _b32tab2 = [a + b for a in _b32tab for b in _b32tab] |
| 249 | 6220 other: 3602.8 KiB |
| 250 | Total allocated size: 5303.1 KiB |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 251 | |
| 252 | See :meth:`Snapshot.statistics` for more options. |
| 253 | |
| 254 | |
| 255 | API |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 256 | --- |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 257 | |
| 258 | Functions |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 259 | ^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 260 | |
| 261 | .. function:: clear_traces() |
| 262 | |
| 263 | Clear traces of memory blocks allocated by Python. |
| 264 | |
| 265 | See also :func:`stop`. |
| 266 | |
| 267 | |
| 268 | .. function:: get_object_traceback(obj) |
| 269 | |
| 270 | Get the traceback where the Python object *obj* was allocated. |
| 271 | Return a :class:`Traceback` instance, or ``None`` if the :mod:`tracemalloc` |
| 272 | module is not tracing memory allocations or did not trace the allocation of |
| 273 | the object. |
| 274 | |
| 275 | See also :func:`gc.get_referrers` and :func:`sys.getsizeof` functions. |
| 276 | |
| 277 | |
| 278 | .. function:: get_traceback_limit() |
| 279 | |
| 280 | Get the maximum number of frames stored in the traceback of a trace. |
| 281 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 282 | The :mod:`tracemalloc` module must be tracing memory allocations to |
| 283 | get the limit, otherwise an exception is raised. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 284 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 285 | The limit is set by the :func:`start` function. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 286 | |
| 287 | |
| 288 | .. function:: get_traced_memory() |
| 289 | |
Victor Stinner | 3c0481d | 2013-11-27 21:39:49 +0100 | [diff] [blame] | 290 | Get the current size and peak size of memory blocks traced by the |
| 291 | :mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 292 | |
| 293 | |
| 294 | .. function:: get_tracemalloc_memory() |
| 295 | |
| 296 | Get the memory usage in bytes of the :mod:`tracemalloc` module used to store |
| 297 | traces of memory blocks. |
| 298 | Return an :class:`int`. |
| 299 | |
| 300 | |
| 301 | .. function:: is_tracing() |
| 302 | |
| 303 | ``True`` if the :mod:`tracemalloc` module is tracing Python memory |
| 304 | allocations, ``False`` otherwise. |
| 305 | |
| 306 | See also :func:`start` and :func:`stop` functions. |
| 307 | |
| 308 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 309 | .. function:: start(nframe: int=1) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 310 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 311 | Start tracing Python memory allocations: install hooks on Python memory |
| 312 | allocators. Collected tracebacks of traces will be limited to *nframe* |
| 313 | frames. By default, a trace of a memory block only stores the most recent |
| 314 | frame: the limit is ``1``. *nframe* must be greater or equal to ``1``. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 315 | |
| 316 | Storing more than ``1`` frame is only useful to compute statistics grouped |
| 317 | by ``'traceback'`` or to compute cumulative statistics: see the |
| 318 | :meth:`Snapshot.compare_to` and :meth:`Snapshot.statistics` methods. |
| 319 | |
| 320 | Storing more frames increases the memory and CPU overhead of the |
| 321 | :mod:`tracemalloc` module. Use the :func:`get_tracemalloc_memory` function |
| 322 | to measure how much memory is used by the :mod:`tracemalloc` module. |
| 323 | |
| 324 | The :envvar:`PYTHONTRACEMALLOC` environment variable |
| 325 | (``PYTHONTRACEMALLOC=NFRAME``) and the :option:`-X` ``tracemalloc=NFRAME`` |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 326 | command line option can be used to start tracing at startup. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 327 | |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 328 | See also :func:`stop`, :func:`is_tracing` and :func:`get_traceback_limit` |
| 329 | functions. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 330 | |
| 331 | |
| 332 | .. function:: stop() |
| 333 | |
| 334 | Stop tracing Python memory allocations: uninstall hooks on Python memory |
Victor Stinner | 00773df | 2013-11-26 00:40:10 +0100 | [diff] [blame] | 335 | allocators. Also clears all previously collected traces of memory blocks |
| 336 | allocated by Python. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 337 | |
| 338 | Call :func:`take_snapshot` function to take a snapshot of traces before |
| 339 | clearing them. |
| 340 | |
Victor Stinner | 00773df | 2013-11-26 00:40:10 +0100 | [diff] [blame] | 341 | See also :func:`start`, :func:`is_tracing` and :func:`clear_traces` |
| 342 | functions. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 343 | |
| 344 | |
| 345 | .. function:: take_snapshot() |
| 346 | |
| 347 | Take a snapshot of traces of memory blocks allocated by Python. Return a new |
| 348 | :class:`Snapshot` instance. |
| 349 | |
| 350 | The snapshot does not include memory blocks allocated before the |
| 351 | :mod:`tracemalloc` module started to trace memory allocations. |
| 352 | |
| 353 | Tracebacks of traces are limited to :func:`get_traceback_limit` frames. Use |
Victor Stinner | 3728d6c | 2013-11-23 12:37:20 +0100 | [diff] [blame] | 354 | the *nframe* parameter of the :func:`start` function to store more frames. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 355 | |
| 356 | The :mod:`tracemalloc` module must be tracing memory allocations to take a |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 357 | snapshot, see the :func:`start` function. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 358 | |
| 359 | See also the :func:`get_object_traceback` function. |
| 360 | |
| 361 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 362 | DomainFilter |
| 363 | ^^^^^^^^^^^^ |
| 364 | |
| 365 | .. class:: DomainFilter(inclusive: bool, domain: int) |
| 366 | |
| 367 | Filter traces of memory blocks by their address space (domain). |
| 368 | |
| 369 | .. versionadded:: 3.6 |
| 370 | |
| 371 | .. attribute:: inclusive |
| 372 | |
| 373 | If *inclusive* is ``True`` (include), match memory blocks allocated |
| 374 | in the address space :attr:`domain`. |
| 375 | |
| 376 | If *inclusive* is ``False`` (exclude), match memory blocks not allocated |
| 377 | in the address space :attr:`domain`. |
| 378 | |
| 379 | .. attribute:: domain |
| 380 | |
| 381 | Address space of a memory block (``int``). Read-only property. |
| 382 | |
| 383 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 384 | Filter |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 385 | ^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 386 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 387 | .. class:: Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False, domain: int=None) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 388 | |
| 389 | Filter on traces of memory blocks. |
| 390 | |
| 391 | See the :func:`fnmatch.fnmatch` function for the syntax of |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 392 | *filename_pattern*. The ``'.pyc'`` file extension is |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 393 | replaced with ``'.py'``. |
| 394 | |
| 395 | Examples: |
| 396 | |
| 397 | * ``Filter(True, subprocess.__file__)`` only includes traces of the |
| 398 | :mod:`subprocess` module |
| 399 | * ``Filter(False, tracemalloc.__file__)`` excludes traces of the |
| 400 | :mod:`tracemalloc` module |
| 401 | * ``Filter(False, "<unknown>")`` excludes empty tracebacks |
| 402 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 403 | |
| 404 | .. versionchanged:: 3.5 |
| 405 | The ``'.pyo'`` file extension is no longer replaced with ``'.py'``. |
| 406 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 407 | .. versionchanged:: 3.6 |
| 408 | Added the :attr:`domain` attribute. |
| 409 | |
| 410 | |
| 411 | .. attribute:: domain |
| 412 | |
| 413 | Address space of a memory block (``int`` or ``None``). |
| 414 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 415 | tracemalloc uses the domain ``0`` to trace memory allocations made by |
| 416 | Python. C extensions can use other domains to trace other resources. |
| 417 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 418 | .. attribute:: inclusive |
| 419 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 420 | If *inclusive* is ``True`` (include), only match memory blocks allocated |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 421 | in a file with a name matching :attr:`filename_pattern` at line number |
| 422 | :attr:`lineno`. |
| 423 | |
| 424 | If *inclusive* is ``False`` (exclude), ignore memory blocks allocated in |
| 425 | a file with a name matching :attr:`filename_pattern` at line number |
| 426 | :attr:`lineno`. |
| 427 | |
| 428 | .. attribute:: lineno |
| 429 | |
| 430 | Line number (``int``) of the filter. If *lineno* is ``None``, the filter |
| 431 | matches any line number. |
| 432 | |
| 433 | .. attribute:: filename_pattern |
| 434 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 435 | Filename pattern of the filter (``str``). Read-only property. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 436 | |
| 437 | .. attribute:: all_frames |
| 438 | |
| 439 | If *all_frames* is ``True``, all frames of the traceback are checked. If |
| 440 | *all_frames* is ``False``, only the most recent frame is checked. |
| 441 | |
Victor Stinner | 5362abf | 2013-11-27 23:39:55 +0100 | [diff] [blame] | 442 | This attribute has no effect if the traceback limit is ``1``. See the |
| 443 | :func:`get_traceback_limit` function and :attr:`Snapshot.traceback_limit` |
| 444 | attribute. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 445 | |
| 446 | |
| 447 | Frame |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 448 | ^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 449 | |
| 450 | .. class:: Frame |
| 451 | |
| 452 | Frame of a traceback. |
| 453 | |
| 454 | The :class:`Traceback` class is a sequence of :class:`Frame` instances. |
| 455 | |
| 456 | .. attribute:: filename |
| 457 | |
| 458 | Filename (``str``). |
| 459 | |
| 460 | .. attribute:: lineno |
| 461 | |
| 462 | Line number (``int``). |
| 463 | |
| 464 | |
| 465 | Snapshot |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 466 | ^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 467 | |
| 468 | .. class:: Snapshot |
| 469 | |
| 470 | Snapshot of traces of memory blocks allocated by Python. |
| 471 | |
| 472 | The :func:`take_snapshot` function creates a snapshot instance. |
| 473 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 474 | .. method:: compare_to(old_snapshot: Snapshot, key_type: str, cumulative: bool=False) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 475 | |
| 476 | Compute the differences with an old snapshot. Get statistics as a sorted |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 477 | list of :class:`StatisticDiff` instances grouped by *key_type*. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 478 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 479 | See the :meth:`Snapshot.statistics` method for *key_type* and *cumulative* |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 480 | parameters. |
| 481 | |
| 482 | The result is sorted from the biggest to the smallest by: absolute value |
| 483 | of :attr:`StatisticDiff.size_diff`, :attr:`StatisticDiff.size`, absolute |
| 484 | value of :attr:`StatisticDiff.count_diff`, :attr:`Statistic.count` and |
| 485 | then by :attr:`StatisticDiff.traceback`. |
| 486 | |
| 487 | |
| 488 | .. method:: dump(filename) |
| 489 | |
| 490 | Write the snapshot into a file. |
| 491 | |
| 492 | Use :meth:`load` to reload the snapshot. |
| 493 | |
| 494 | |
| 495 | .. method:: filter_traces(filters) |
| 496 | |
| 497 | Create a new :class:`Snapshot` instance with a filtered :attr:`traces` |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 498 | sequence, *filters* is a list of :class:`DomainFilter` and |
| 499 | :class:`Filter` instances. If *filters* is an empty list, return a new |
| 500 | :class:`Snapshot` instance with a copy of the traces. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 501 | |
| 502 | All inclusive filters are applied at once, a trace is ignored if no |
| 503 | inclusive filters match it. A trace is ignored if at least one exclusive |
Martin Panter | 1f1177d | 2015-10-31 11:48:53 +0000 | [diff] [blame] | 504 | filter matches it. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 505 | |
Victor Stinner | e492ae5 | 2016-03-22 12:58:23 +0100 | [diff] [blame] | 506 | .. versionchanged:: 3.6 |
| 507 | :class:`DomainFilter` instances are now also accepted in *filters*. |
| 508 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 509 | |
| 510 | .. classmethod:: load(filename) |
| 511 | |
| 512 | Load a snapshot from a file. |
| 513 | |
| 514 | See also :meth:`dump`. |
| 515 | |
| 516 | |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 517 | .. method:: statistics(key_type: str, cumulative: bool=False) |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 518 | |
| 519 | Get statistics as a sorted list of :class:`Statistic` instances grouped |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 520 | by *key_type*: |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 521 | |
| 522 | ===================== ======================== |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 523 | key_type description |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 524 | ===================== ======================== |
| 525 | ``'filename'`` filename |
| 526 | ``'lineno'`` filename and line number |
| 527 | ``'traceback'`` traceback |
| 528 | ===================== ======================== |
| 529 | |
| 530 | If *cumulative* is ``True``, cumulate size and count of memory blocks of |
| 531 | all frames of the traceback of a trace, not only the most recent frame. |
Victor Stinner | 440d703 | 2016-12-30 02:14:59 +0100 | [diff] [blame] | 532 | The cumulative mode can only be used with *key_type* equals to |
Victor Stinner | 8e3708d | 2013-11-26 00:45:47 +0100 | [diff] [blame] | 533 | ``'filename'`` and ``'lineno'``. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 534 | |
| 535 | The result is sorted from the biggest to the smallest by: |
| 536 | :attr:`Statistic.size`, :attr:`Statistic.count` and then by |
| 537 | :attr:`Statistic.traceback`. |
| 538 | |
| 539 | |
| 540 | .. attribute:: traceback_limit |
| 541 | |
| 542 | Maximum number of frames stored in the traceback of :attr:`traces`: |
| 543 | result of the :func:`get_traceback_limit` when the snapshot was taken. |
| 544 | |
| 545 | .. attribute:: traces |
| 546 | |
| 547 | Traces of all memory blocks allocated by Python: sequence of |
| 548 | :class:`Trace` instances. |
| 549 | |
| 550 | The sequence has an undefined order. Use the :meth:`Snapshot.statistics` |
| 551 | method to get a sorted list of statistics. |
| 552 | |
| 553 | |
| 554 | Statistic |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 555 | ^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 556 | |
| 557 | .. class:: Statistic |
| 558 | |
| 559 | Statistic on memory allocations. |
| 560 | |
| 561 | :func:`Snapshot.statistics` returns a list of :class:`Statistic` instances. |
| 562 | |
| 563 | See also the :class:`StatisticDiff` class. |
| 564 | |
| 565 | .. attribute:: count |
| 566 | |
| 567 | Number of memory blocks (``int``). |
| 568 | |
| 569 | .. attribute:: size |
| 570 | |
| 571 | Total size of memory blocks in bytes (``int``). |
| 572 | |
| 573 | .. attribute:: traceback |
| 574 | |
| 575 | Traceback where the memory block was allocated, :class:`Traceback` |
| 576 | instance. |
| 577 | |
| 578 | |
| 579 | StatisticDiff |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 580 | ^^^^^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 581 | |
| 582 | .. class:: StatisticDiff |
| 583 | |
| 584 | Statistic difference on memory allocations between an old and a new |
| 585 | :class:`Snapshot` instance. |
| 586 | |
| 587 | :func:`Snapshot.compare_to` returns a list of :class:`StatisticDiff` |
| 588 | instances. See also the :class:`Statistic` class. |
| 589 | |
| 590 | .. attribute:: count |
| 591 | |
| 592 | Number of memory blocks in the new snapshot (``int``): ``0`` if |
| 593 | the memory blocks have been released in the new snapshot. |
| 594 | |
| 595 | .. attribute:: count_diff |
| 596 | |
| 597 | Difference of number of memory blocks between the old and the new |
| 598 | snapshots (``int``): ``0`` if the memory blocks have been allocated in |
| 599 | the new snapshot. |
| 600 | |
| 601 | .. attribute:: size |
| 602 | |
| 603 | Total size of memory blocks in bytes in the new snapshot (``int``): |
| 604 | ``0`` if the memory blocks have been released in the new snapshot. |
| 605 | |
| 606 | .. attribute:: size_diff |
| 607 | |
| 608 | Difference of total size of memory blocks in bytes between the old and |
| 609 | the new snapshots (``int``): ``0`` if the memory blocks have been |
| 610 | allocated in the new snapshot. |
| 611 | |
| 612 | .. attribute:: traceback |
| 613 | |
| 614 | Traceback where the memory blocks were allocated, :class:`Traceback` |
| 615 | instance. |
| 616 | |
| 617 | |
| 618 | Trace |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 619 | ^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 620 | |
| 621 | .. class:: Trace |
| 622 | |
| 623 | Trace of a memory block. |
| 624 | |
| 625 | The :attr:`Snapshot.traces` attribute is a sequence of :class:`Trace` |
| 626 | instances. |
| 627 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 628 | .. versionchanged:: 3.6 |
| 629 | Added the :attr:`domain` attribute. |
| 630 | |
| 631 | .. attribute:: domain |
| 632 | |
| 633 | Address space of a memory block (``int``). Read-only property. |
| 634 | |
| 635 | tracemalloc uses the domain ``0`` to trace memory allocations made by |
| 636 | Python. C extensions can use other domains to trace other resources. |
| 637 | |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 638 | .. attribute:: size |
| 639 | |
| 640 | Size of the memory block in bytes (``int``). |
| 641 | |
| 642 | .. attribute:: traceback |
| 643 | |
| 644 | Traceback where the memory block was allocated, :class:`Traceback` |
| 645 | instance. |
| 646 | |
| 647 | |
| 648 | Traceback |
Georg Brandl | 717e028 | 2014-10-31 10:21:07 +0100 | [diff] [blame] | 649 | ^^^^^^^^^ |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 650 | |
| 651 | .. class:: Traceback |
| 652 | |
Jesse-Bakker | 706e10b | 2017-11-30 00:05:07 +0100 | [diff] [blame] | 653 | Sequence of :class:`Frame` instances sorted from the oldest frame to the |
| 654 | most recent frame. |
Victor Stinner | ed3b0bc | 2013-11-23 12:27:24 +0100 | [diff] [blame] | 655 | |
| 656 | A traceback contains at least ``1`` frame. If the ``tracemalloc`` module |
| 657 | failed to get a frame, the filename ``"<unknown>"`` at line number ``0`` is |
| 658 | used. |
| 659 | |
| 660 | When a snapshot is taken, tracebacks of traces are limited to |
| 661 | :func:`get_traceback_limit` frames. See the :func:`take_snapshot` function. |
| 662 | |
| 663 | The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback` |
| 664 | instance. |
| 665 | |
Jesse-Bakker | 706e10b | 2017-11-30 00:05:07 +0100 | [diff] [blame] | 666 | .. versionchanged:: 3.7 |
| 667 | Frames are now sorted from the oldest to the most recent, instead of most recent to oldest. |
Victor Stinner | 23f628d | 2014-02-16 23:53:38 +0100 | [diff] [blame] | 668 | |
Jesse-Bakker | 706e10b | 2017-11-30 00:05:07 +0100 | [diff] [blame] | 669 | .. method:: format(limit=None, most_recent_first=False) |
| 670 | |
| 671 | Format the traceback as a list of lines with newlines. Use the |
| 672 | :mod:`linecache` module to retrieve lines from the source code. |
| 673 | If *limit* is set, format the *limit* most recent frames if *limit* |
| 674 | is positive. Otherwise, format the ``abs(limit)`` oldest frames. |
| 675 | If *most_recent_first* is ``True``, the order of the formatted frames |
| 676 | is reversed, returning the most recent frame first instead of last. |
Victor Stinner | 23f628d | 2014-02-16 23:53:38 +0100 | [diff] [blame] | 677 | |
| 678 | Similar to the :func:`traceback.format_tb` function, except that |
Martin Panter | d5db147 | 2016-02-08 01:34:09 +0000 | [diff] [blame] | 679 | :meth:`.format` does not include newlines. |
Victor Stinner | 23f628d | 2014-02-16 23:53:38 +0100 | [diff] [blame] | 680 | |
| 681 | Example:: |
| 682 | |
| 683 | print("Traceback (most recent call first):") |
| 684 | for line in traceback: |
| 685 | print(line) |
| 686 | |
| 687 | Output:: |
| 688 | |
| 689 | Traceback (most recent call first): |
| 690 | File "test.py", line 9 |
| 691 | obj = Object() |
| 692 | File "test.py", line 12 |
| 693 | tb = tracemalloc.get_object_traceback(f()) |