blob: c12ef8785ea33459d44176472ca897f9460b8342 [file] [log] [blame]
Victor Stinnered3b0bc2013-11-23 12:27:24 +01001:mod:`tracemalloc` --- Trace memory allocations
2===============================================
3
4.. module:: tracemalloc
5 :synopsis: Trace memory allocations.
6
R David Murray6d5cb072013-12-20 14:48:50 -05007.. versionadded:: 3.4
8
Victor Stinnered3b0bc2013-11-23 12:27:24 +01009The tracemalloc module is a debug tool to trace memory blocks allocated by
10Python. It provides the following information:
11
12* Traceback where an object was allocated
13* Statistics on allocated memory blocks per filename and per line number:
14 total size, number and average size of allocated memory blocks
15* Compute the differences between two snapshots to detect memory leaks
16
17To trace most memory blocks allocated by Python, the module should be started
18as early as possible by setting the :envvar:`PYTHONTRACEMALLOC` environment
19variable to ``1``, or by using :option:`-X` ``tracemalloc`` command line
20option. The :func:`tracemalloc.start` function can be called at runtime to
21start tracing Python memory allocations.
22
23By default, a trace of an allocated memory block only stores the most recent
24frame (1 frame). To store 25 frames at startup: set the
25:envvar:`PYTHONTRACEMALLOC` environment variable to ``25``, or use the
Victor Stinner3728d6c2013-11-23 12:37:20 +010026:option:`-X` ``tracemalloc=25`` command line option.
Victor Stinnered3b0bc2013-11-23 12:27:24 +010027
Victor Stinnered3b0bc2013-11-23 12:27:24 +010028
29Examples
30========
31
32Display the top 10
33------------------
34
35Display the 10 files allocating the most memory::
36
37 import tracemalloc
38
39 tracemalloc.start()
40
41 # ... run your application ...
42
43 snapshot = tracemalloc.take_snapshot()
44 top_stats = snapshot.statistics('lineno')
45
46 print("[ Top 10 ]")
47 for stat in top_stats[:10]:
48 print(stat)
49
50
51Example of output of the Python test suite::
52
53 [ Top 10 ]
54 <frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B
55 <frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 B
56 /usr/lib/python3.4/collections/__init__.py:368: size=244 KiB, count=2315, average=108 B
57 /usr/lib/python3.4/unittest/case.py:381: size=185 KiB, count=779, average=243 B
58 /usr/lib/python3.4/unittest/case.py:402: size=154 KiB, count=378, average=416 B
59 /usr/lib/python3.4/abc.py:133: size=88.7 KiB, count=347, average=262 B
60 <frozen importlib._bootstrap>:1446: size=70.4 KiB, count=911, average=79 B
61 <frozen importlib._bootstrap>:1454: size=52.0 KiB, count=25, average=2131 B
62 <string>:5: size=49.7 KiB, count=148, average=344 B
63 /usr/lib/python3.4/sysconfig.py:411: size=48.0 KiB, count=1, average=48.0 KiB
64
65We can see that Python loaded ``4.8 MiB`` data (bytecode and constants) from
66modules and that the :mod:`collections` module allocated ``244 KiB`` to build
67:class:`~collections.namedtuple` types.
68
69See :meth:`Snapshot.statistics` for more options.
70
71
72Compute differences
73-------------------
74
75Take two snapshots and display the differences::
76
77 import tracemalloc
78 tracemalloc.start()
79 # ... start your application ...
80
81 snapshot1 = tracemalloc.take_snapshot()
82 # ... call the function leaking memory ...
83 snapshot2 = tracemalloc.take_snapshot()
84
85 top_stats = snapshot2.compare_to(snapshot1, 'lineno')
86
87 print("[ Top 10 differences ]")
88 for stat in top_stats[:10]:
89 print(stat)
90
91Example of output before/after running some tests of the Python test suite::
92
93 [ Top 10 differences ]
94 <frozen importlib._bootstrap>:716: size=8173 KiB (+4428 KiB), count=71332 (+39369), average=117 B
95 /usr/lib/python3.4/linecache.py:127: size=940 KiB (+940 KiB), count=8106 (+8106), average=119 B
96 /usr/lib/python3.4/unittest/case.py:571: size=298 KiB (+298 KiB), count=589 (+589), average=519 B
97 <frozen importlib._bootstrap>:284: size=1005 KiB (+166 KiB), count=7423 (+1526), average=139 B
98 /usr/lib/python3.4/mimetypes.py:217: size=112 KiB (+112 KiB), count=1334 (+1334), average=86 B
99 /usr/lib/python3.4/http/server.py:848: size=96.0 KiB (+96.0 KiB), count=1 (+1), average=96.0 KiB
100 /usr/lib/python3.4/inspect.py:1465: size=83.5 KiB (+83.5 KiB), count=109 (+109), average=784 B
101 /usr/lib/python3.4/unittest/mock.py:491: size=77.7 KiB (+77.7 KiB), count=143 (+143), average=557 B
102 /usr/lib/python3.4/urllib/parse.py:476: size=71.8 KiB (+71.8 KiB), count=969 (+969), average=76 B
103 /usr/lib/python3.4/contextlib.py:38: size=67.2 KiB (+67.2 KiB), count=126 (+126), average=546 B
104
Victor Stinner00773df2013-11-26 00:40:10 +0100105We can see that Python has loaded ``8.2 MiB`` of module data (bytecode and
106constants), and that this is ``4.4 MiB`` more than had been loaded before the
107tests, when the previous snapshot was taken. Similarly, the :mod:`linecache`
108module has cached ``940 KiB`` of Python source code to format tracebacks, all
109of it since the previous snapshot.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100110
111If the system has little free memory, snapshots can be written on disk using
112the :meth:`Snapshot.dump` method to analyze the snapshot offline. Then use the
113:meth:`Snapshot.load` method reload the snapshot.
114
115
116Get the traceback of a memory block
117-----------------------------------
118
119Code to display the traceback of the biggest memory block::
120
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100121 import tracemalloc
122
Victor Stinner3728d6c2013-11-23 12:37:20 +0100123 # Store 25 frames
124 tracemalloc.start(25)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100125
126 # ... run your application ...
127
128 snapshot = tracemalloc.take_snapshot()
129 top_stats = snapshot.statistics('traceback')
130
131 # pick the biggest memory block
132 stat = top_stats[0]
133 print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
Victor Stinner23f628d2014-02-16 23:53:38 +0100134 for line in stat.traceback.format():
135 print(line)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100136
137Example of output of the Python test suite (traceback limited to 25 frames)::
138
139 903 memory blocks: 870.1 KiB
140 File "<frozen importlib._bootstrap>", line 716
141 File "<frozen importlib._bootstrap>", line 1036
142 File "<frozen importlib._bootstrap>", line 934
143 File "<frozen importlib._bootstrap>", line 1068
144 File "<frozen importlib._bootstrap>", line 619
145 File "<frozen importlib._bootstrap>", line 1581
146 File "<frozen importlib._bootstrap>", line 1614
147 File "/usr/lib/python3.4/doctest.py", line 101
148 import pdb
149 File "<frozen importlib._bootstrap>", line 284
150 File "<frozen importlib._bootstrap>", line 938
151 File "<frozen importlib._bootstrap>", line 1068
152 File "<frozen importlib._bootstrap>", line 619
153 File "<frozen importlib._bootstrap>", line 1581
154 File "<frozen importlib._bootstrap>", line 1614
155 File "/usr/lib/python3.4/test/support/__init__.py", line 1728
156 import doctest
157 File "/usr/lib/python3.4/test/test_pickletools.py", line 21
158 support.run_doctest(pickletools)
159 File "/usr/lib/python3.4/test/regrtest.py", line 1276
160 test_runner()
161 File "/usr/lib/python3.4/test/regrtest.py", line 976
162 display_failure=not verbose)
163 File "/usr/lib/python3.4/test/regrtest.py", line 761
164 match_tests=ns.match_tests)
165 File "/usr/lib/python3.4/test/regrtest.py", line 1563
166 main()
167 File "/usr/lib/python3.4/test/__main__.py", line 3
168 regrtest.main_in_temp_cwd()
169 File "/usr/lib/python3.4/runpy.py", line 73
170 exec(code, run_globals)
171 File "/usr/lib/python3.4/runpy.py", line 160
172 "__main__", fname, loader, pkg_name)
173
Victor Stinner00773df2013-11-26 00:40:10 +0100174We can see that the most memory was allocated in the :mod:`importlib` module to
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100175load data (bytecode and constants) from modules: ``870 KiB``. The traceback is
Victor Stinner00773df2013-11-26 00:40:10 +0100176where the :mod:`importlib` loaded data most recently: on the ``import pdb``
177line of the :mod:`doctest` module. The traceback may change if a new module is
178loaded.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100179
180
181Pretty top
182----------
183
184Code to display the 10 lines allocating the most memory with a pretty output,
185ignoring ``<frozen importlib._bootstrap>`` and ``<unknown>`` files::
186
187 import os
188 import tracemalloc
189
190 def display_top(snapshot, group_by='lineno', limit=10):
191 snapshot = snapshot.filter_traces((
192 tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
193 tracemalloc.Filter(False, "<unknown>"),
194 ))
195 top_stats = snapshot.statistics(group_by)
196
197 print("Top %s lines" % limit)
198 for index, stat in enumerate(top_stats[:limit], 1):
199 frame = stat.traceback[0]
200 # replace "/path/to/module/file.py" with "module/file.py"
201 filename = os.sep.join(frame.filename.split(os.sep)[-2:])
202 print("#%s: %s:%s: %.1f KiB"
Victor Stinnerf70200e2014-03-03 11:57:57 +0100203 % (index, filename, frame.lineno, stat.size / 1024))
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100204
205 other = top_stats[limit:]
206 if other:
207 size = sum(stat.size for stat in other)
208 print("%s other: %.1f KiB" % (len(other), size / 1024))
209 total = sum(stat.size for stat in top_stats)
210 print("Total allocated size: %.1f KiB" % (total / 1024))
211
212 tracemalloc.start()
213
214 # ... run your application ...
215
216 snapshot = tracemalloc.take_snapshot()
Victor Stinnerf70200e2014-03-03 11:57:57 +0100217 display_top(snapshot)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100218
219Example of output of the Python test suite::
220
221 2013-11-08 14:16:58.149320: Top 10 lines
222 #1: collections/__init__.py:368: 291.9 KiB
223 #2: Lib/doctest.py:1291: 200.2 KiB
224 #3: unittest/case.py:571: 160.3 KiB
225 #4: Lib/abc.py:133: 99.8 KiB
226 #5: urllib/parse.py:476: 71.8 KiB
227 #6: <string>:5: 62.7 KiB
228 #7: Lib/base64.py:140: 59.8 KiB
229 #8: Lib/_weakrefset.py:37: 51.8 KiB
230 #9: collections/__init__.py:362: 50.6 KiB
231 #10: test/test_site.py:56: 48.0 KiB
232 7496 other: 4161.9 KiB
233 Total allocated size: 5258.8 KiB
234
235See :meth:`Snapshot.statistics` for more options.
236
237
238API
239===
240
241Functions
242---------
243
244.. function:: clear_traces()
245
246 Clear traces of memory blocks allocated by Python.
247
248 See also :func:`stop`.
249
250
251.. function:: get_object_traceback(obj)
252
253 Get the traceback where the Python object *obj* was allocated.
254 Return a :class:`Traceback` instance, or ``None`` if the :mod:`tracemalloc`
255 module is not tracing memory allocations or did not trace the allocation of
256 the object.
257
258 See also :func:`gc.get_referrers` and :func:`sys.getsizeof` functions.
259
260
261.. function:: get_traceback_limit()
262
263 Get the maximum number of frames stored in the traceback of a trace.
264
Victor Stinner3728d6c2013-11-23 12:37:20 +0100265 The :mod:`tracemalloc` module must be tracing memory allocations to
266 get the limit, otherwise an exception is raised.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100267
Victor Stinner3728d6c2013-11-23 12:37:20 +0100268 The limit is set by the :func:`start` function.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100269
270
271.. function:: get_traced_memory()
272
Victor Stinner3c0481d2013-11-27 21:39:49 +0100273 Get the current size and peak size of memory blocks traced by the
274 :mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100275
276
277.. function:: get_tracemalloc_memory()
278
279 Get the memory usage in bytes of the :mod:`tracemalloc` module used to store
280 traces of memory blocks.
281 Return an :class:`int`.
282
283
284.. function:: is_tracing()
285
286 ``True`` if the :mod:`tracemalloc` module is tracing Python memory
287 allocations, ``False`` otherwise.
288
289 See also :func:`start` and :func:`stop` functions.
290
291
Victor Stinner3728d6c2013-11-23 12:37:20 +0100292.. function:: start(nframe: int=1)
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100293
Victor Stinner3728d6c2013-11-23 12:37:20 +0100294 Start tracing Python memory allocations: install hooks on Python memory
295 allocators. Collected tracebacks of traces will be limited to *nframe*
296 frames. By default, a trace of a memory block only stores the most recent
297 frame: the limit is ``1``. *nframe* must be greater or equal to ``1``.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100298
299 Storing more than ``1`` frame is only useful to compute statistics grouped
300 by ``'traceback'`` or to compute cumulative statistics: see the
301 :meth:`Snapshot.compare_to` and :meth:`Snapshot.statistics` methods.
302
303 Storing more frames increases the memory and CPU overhead of the
304 :mod:`tracemalloc` module. Use the :func:`get_tracemalloc_memory` function
305 to measure how much memory is used by the :mod:`tracemalloc` module.
306
307 The :envvar:`PYTHONTRACEMALLOC` environment variable
308 (``PYTHONTRACEMALLOC=NFRAME``) and the :option:`-X` ``tracemalloc=NFRAME``
Victor Stinner3728d6c2013-11-23 12:37:20 +0100309 command line option can be used to start tracing at startup.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100310
Victor Stinner3728d6c2013-11-23 12:37:20 +0100311 See also :func:`stop`, :func:`is_tracing` and :func:`get_traceback_limit`
312 functions.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100313
314
315.. function:: stop()
316
317 Stop tracing Python memory allocations: uninstall hooks on Python memory
Victor Stinner00773df2013-11-26 00:40:10 +0100318 allocators. Also clears all previously collected traces of memory blocks
319 allocated by Python.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100320
321 Call :func:`take_snapshot` function to take a snapshot of traces before
322 clearing them.
323
Victor Stinner00773df2013-11-26 00:40:10 +0100324 See also :func:`start`, :func:`is_tracing` and :func:`clear_traces`
325 functions.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100326
327
328.. function:: take_snapshot()
329
330 Take a snapshot of traces of memory blocks allocated by Python. Return a new
331 :class:`Snapshot` instance.
332
333 The snapshot does not include memory blocks allocated before the
334 :mod:`tracemalloc` module started to trace memory allocations.
335
336 Tracebacks of traces are limited to :func:`get_traceback_limit` frames. Use
Victor Stinner3728d6c2013-11-23 12:37:20 +0100337 the *nframe* parameter of the :func:`start` function to store more frames.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100338
339 The :mod:`tracemalloc` module must be tracing memory allocations to take a
340 snapshot, see the the :func:`start` function.
341
342 See also the :func:`get_object_traceback` function.
343
344
345Filter
346------
347
348.. class:: Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False)
349
350 Filter on traces of memory blocks.
351
352 See the :func:`fnmatch.fnmatch` function for the syntax of
353 *filename_pattern*. The ``'.pyc'`` and ``'.pyo'`` file extensions are
354 replaced with ``'.py'``.
355
356 Examples:
357
358 * ``Filter(True, subprocess.__file__)`` only includes traces of the
359 :mod:`subprocess` module
360 * ``Filter(False, tracemalloc.__file__)`` excludes traces of the
361 :mod:`tracemalloc` module
362 * ``Filter(False, "<unknown>")`` excludes empty tracebacks
363
364 .. attribute:: inclusive
365
366 If *inclusive* is ``True`` (include), only trace memory blocks allocated
367 in a file with a name matching :attr:`filename_pattern` at line number
368 :attr:`lineno`.
369
370 If *inclusive* is ``False`` (exclude), ignore memory blocks allocated in
371 a file with a name matching :attr:`filename_pattern` at line number
372 :attr:`lineno`.
373
374 .. attribute:: lineno
375
376 Line number (``int``) of the filter. If *lineno* is ``None``, the filter
377 matches any line number.
378
379 .. attribute:: filename_pattern
380
381 Filename pattern of the filter (``str``).
382
383 .. attribute:: all_frames
384
385 If *all_frames* is ``True``, all frames of the traceback are checked. If
386 *all_frames* is ``False``, only the most recent frame is checked.
387
Victor Stinner5362abf2013-11-27 23:39:55 +0100388 This attribute has no effect if the traceback limit is ``1``. See the
389 :func:`get_traceback_limit` function and :attr:`Snapshot.traceback_limit`
390 attribute.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100391
392
393Frame
394-----
395
396.. class:: Frame
397
398 Frame of a traceback.
399
400 The :class:`Traceback` class is a sequence of :class:`Frame` instances.
401
402 .. attribute:: filename
403
404 Filename (``str``).
405
406 .. attribute:: lineno
407
408 Line number (``int``).
409
410
411Snapshot
412--------
413
414.. class:: Snapshot
415
416 Snapshot of traces of memory blocks allocated by Python.
417
418 The :func:`take_snapshot` function creates a snapshot instance.
419
420 .. method:: compare_to(old_snapshot: Snapshot, group_by: str, cumulative: bool=False)
421
422 Compute the differences with an old snapshot. Get statistics as a sorted
423 list of :class:`StatisticDiff` instances grouped by *group_by*.
424
425 See the :meth:`statistics` method for *group_by* and *cumulative*
426 parameters.
427
428 The result is sorted from the biggest to the smallest by: absolute value
429 of :attr:`StatisticDiff.size_diff`, :attr:`StatisticDiff.size`, absolute
430 value of :attr:`StatisticDiff.count_diff`, :attr:`Statistic.count` and
431 then by :attr:`StatisticDiff.traceback`.
432
433
434 .. method:: dump(filename)
435
436 Write the snapshot into a file.
437
438 Use :meth:`load` to reload the snapshot.
439
440
441 .. method:: filter_traces(filters)
442
443 Create a new :class:`Snapshot` instance with a filtered :attr:`traces`
444 sequence, *filters* is a list of :class:`Filter` instances. If *filters*
445 is an empty list, return a new :class:`Snapshot` instance with a copy of
446 the traces.
447
448 All inclusive filters are applied at once, a trace is ignored if no
449 inclusive filters match it. A trace is ignored if at least one exclusive
450 filter matchs it.
451
452
453 .. classmethod:: load(filename)
454
455 Load a snapshot from a file.
456
457 See also :meth:`dump`.
458
459
460 .. method:: statistics(group_by: str, cumulative: bool=False)
461
462 Get statistics as a sorted list of :class:`Statistic` instances grouped
463 by *group_by*:
464
465 ===================== ========================
466 group_by description
467 ===================== ========================
468 ``'filename'`` filename
469 ``'lineno'`` filename and line number
470 ``'traceback'`` traceback
471 ===================== ========================
472
473 If *cumulative* is ``True``, cumulate size and count of memory blocks of
474 all frames of the traceback of a trace, not only the most recent frame.
475 The cumulative mode can only be used with *group_by* equals to
Victor Stinner8e3708d2013-11-26 00:45:47 +0100476 ``'filename'`` and ``'lineno'``.
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100477
478 The result is sorted from the biggest to the smallest by:
479 :attr:`Statistic.size`, :attr:`Statistic.count` and then by
480 :attr:`Statistic.traceback`.
481
482
483 .. attribute:: traceback_limit
484
485 Maximum number of frames stored in the traceback of :attr:`traces`:
486 result of the :func:`get_traceback_limit` when the snapshot was taken.
487
488 .. attribute:: traces
489
490 Traces of all memory blocks allocated by Python: sequence of
491 :class:`Trace` instances.
492
493 The sequence has an undefined order. Use the :meth:`Snapshot.statistics`
494 method to get a sorted list of statistics.
495
496
497Statistic
498---------
499
500.. class:: Statistic
501
502 Statistic on memory allocations.
503
504 :func:`Snapshot.statistics` returns a list of :class:`Statistic` instances.
505
506 See also the :class:`StatisticDiff` class.
507
508 .. attribute:: count
509
510 Number of memory blocks (``int``).
511
512 .. attribute:: size
513
514 Total size of memory blocks in bytes (``int``).
515
516 .. attribute:: traceback
517
518 Traceback where the memory block was allocated, :class:`Traceback`
519 instance.
520
521
522StatisticDiff
523-------------
524
525.. class:: StatisticDiff
526
527 Statistic difference on memory allocations between an old and a new
528 :class:`Snapshot` instance.
529
530 :func:`Snapshot.compare_to` returns a list of :class:`StatisticDiff`
531 instances. See also the :class:`Statistic` class.
532
533 .. attribute:: count
534
535 Number of memory blocks in the new snapshot (``int``): ``0`` if
536 the memory blocks have been released in the new snapshot.
537
538 .. attribute:: count_diff
539
540 Difference of number of memory blocks between the old and the new
541 snapshots (``int``): ``0`` if the memory blocks have been allocated in
542 the new snapshot.
543
544 .. attribute:: size
545
546 Total size of memory blocks in bytes in the new snapshot (``int``):
547 ``0`` if the memory blocks have been released in the new snapshot.
548
549 .. attribute:: size_diff
550
551 Difference of total size of memory blocks in bytes between the old and
552 the new snapshots (``int``): ``0`` if the memory blocks have been
553 allocated in the new snapshot.
554
555 .. attribute:: traceback
556
557 Traceback where the memory blocks were allocated, :class:`Traceback`
558 instance.
559
560
561Trace
562-----
563
564.. class:: Trace
565
566 Trace of a memory block.
567
568 The :attr:`Snapshot.traces` attribute is a sequence of :class:`Trace`
569 instances.
570
571 .. attribute:: size
572
573 Size of the memory block in bytes (``int``).
574
575 .. attribute:: traceback
576
577 Traceback where the memory block was allocated, :class:`Traceback`
578 instance.
579
580
581Traceback
582---------
583
584.. class:: Traceback
585
586 Sequence of :class:`Frame` instances sorted from the most recent frame to
587 the oldest frame.
588
589 A traceback contains at least ``1`` frame. If the ``tracemalloc`` module
590 failed to get a frame, the filename ``"<unknown>"`` at line number ``0`` is
591 used.
592
593 When a snapshot is taken, tracebacks of traces are limited to
594 :func:`get_traceback_limit` frames. See the :func:`take_snapshot` function.
595
596 The :attr:`Trace.traceback` attribute is an instance of :class:`Traceback`
597 instance.
598
Victor Stinner23f628d2014-02-16 23:53:38 +0100599 .. method:: format(limit=None)
600
601 Format the traceback as a list of lines with newlines. Use the
602 :mod:`linecache` module to retrieve lines from the source code. If
603 *limit* is set, only format the *limit* most recent frames.
604
605 Similar to the :func:`traceback.format_tb` function, except that
606 :meth:`format` does not include newlines.
607
608 Example::
609
610 print("Traceback (most recent call first):")
611 for line in traceback:
612 print(line)
613
614 Output::
615
616 Traceback (most recent call first):
617 File "test.py", line 9
618 obj = Object()
619 File "test.py", line 12
620 tb = tracemalloc.get_object_traceback(f())
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100621