Linux iad1-shared-b7-18 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
Apache
: 67.205.6.31 | : 216.73.216.47
Cant Read [ /etc/named.conf ]
8.2.29
fernandoquevedo
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python3.10 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
70.96
KB
-rw-r--r--
__future__.html
20.75
KB
-rw-r--r--
__main__.html
44.87
KB
-rw-r--r--
_thread.html
30.39
KB
-rw-r--r--
abc.html
46.87
KB
-rw-r--r--
aifc.html
35.56
KB
-rw-r--r--
allos.html
34.33
KB
-rw-r--r--
archiving.html
17
KB
-rw-r--r--
argparse.html
300.89
KB
-rw-r--r--
array.html
37.28
KB
-rw-r--r--
ast.html
293.08
KB
-rw-r--r--
asynchat.html
37.41
KB
-rw-r--r--
asyncio-api-index.html
27.68
KB
-rw-r--r--
asyncio-dev.html
36.1
KB
-rw-r--r--
asyncio-eventloop.html
204.43
KB
-rw-r--r--
asyncio-exceptions.html
17.11
KB
-rw-r--r--
asyncio-future.html
38.23
KB
-rw-r--r--
asyncio-llapi-index.html
61.97
KB
-rw-r--r--
asyncio-platforms.html
21.77
KB
-rw-r--r--
asyncio-policy.html
39.56
KB
-rw-r--r--
asyncio-protocol.html
118.33
KB
-rw-r--r--
asyncio-queue.html
31.86
KB
-rw-r--r--
asyncio-stream.html
69.98
KB
-rw-r--r--
asyncio-subprocess.html
49.25
KB
-rw-r--r--
asyncio-sync.html
42.79
KB
-rw-r--r--
asyncio-task.html
109.14
KB
-rw-r--r--
asyncio.html
17.31
KB
-rw-r--r--
asyncore.html
49.36
KB
-rw-r--r--
atexit.html
22.87
KB
-rw-r--r--
audioop.html
46
KB
-rw-r--r--
audit_events.html
72.85
KB
-rw-r--r--
base64.html
48.09
KB
-rw-r--r--
bdb.html
66
KB
-rw-r--r--
binary.html
15.97
KB
-rw-r--r--
binascii.html
36.3
KB
-rw-r--r--
binhex.html
15.97
KB
-rw-r--r--
bisect.html
43.38
KB
-rw-r--r--
builtins.html
14.87
KB
-rw-r--r--
bz2.html
50.68
KB
-rw-r--r--
calendar.html
69.35
KB
-rw-r--r--
cgi.html
59.95
KB
-rw-r--r--
cgitb.html
19.96
KB
-rw-r--r--
chunk.html
22.21
KB
-rw-r--r--
cmath.html
43.89
KB
-rw-r--r--
cmd.html
50.42
KB
-rw-r--r--
code.html
35.38
KB
-rw-r--r--
codecs.html
157.96
KB
-rw-r--r--
codeop.html
18.26
KB
-rw-r--r--
collections.abc.html
79.17
KB
-rw-r--r--
collections.html
189.43
KB
-rw-r--r--
colorsys.html
17.31
KB
-rw-r--r--
compileall.html
50.68
KB
-rw-r--r--
concurrency.html
24.15
KB
-rw-r--r--
concurrent.futures.html
79.03
KB
-rw-r--r--
concurrent.html
11.56
KB
-rw-r--r--
configparser.html
159.4
KB
-rw-r--r--
constants.html
22.41
KB
-rw-r--r--
contextlib.html
117.2
KB
-rw-r--r--
contextvars.html
38.38
KB
-rw-r--r--
copy.html
17.92
KB
-rw-r--r--
copyreg.html
18.63
KB
-rw-r--r--
crypt.html
28.29
KB
-rw-r--r--
crypto.html
13.93
KB
-rw-r--r--
csv.html
82.89
KB
-rw-r--r--
ctypes.html
295.49
KB
-rw-r--r--
curses.ascii.html
32.04
KB
-rw-r--r--
curses.html
221.24
KB
-rw-r--r--
curses.panel.html
22.16
KB
-rw-r--r--
custominterp.html
11.85
KB
-rw-r--r--
dataclasses.html
113.16
KB
-rw-r--r--
datatypes.html
31.82
KB
-rw-r--r--
datetime.html
374.92
KB
-rw-r--r--
dbm.html
51.16
KB
-rw-r--r--
debug.html
17
KB
-rw-r--r--
decimal.html
270.12
KB
-rw-r--r--
development.html
32.34
KB
-rw-r--r--
devmode.html
31.72
KB
-rw-r--r--
dialog.html
43.03
KB
-rw-r--r--
difflib.html
111.99
KB
-rw-r--r--
dis.html
136.07
KB
-rw-r--r--
distribution.html
13.67
KB
-rw-r--r--
distutils.html
14.73
KB
-rw-r--r--
doctest.html
199.32
KB
-rw-r--r--
email.charset.html
31.92
KB
-rw-r--r--
email.compat32-message.html
97.74
KB
-rw-r--r--
email.contentmanager.html
37.02
KB
-rw-r--r--
email.encoders.html
18.47
KB
-rw-r--r--
email.errors.html
24.45
KB
-rw-r--r--
email.examples.html
60.49
KB
-rw-r--r--
email.generator.html
48.79
KB
-rw-r--r--
email.header.html
37.61
KB
-rw-r--r--
email.headerregistry.html
66.26
KB
-rw-r--r--
email.html
30.75
KB
-rw-r--r--
email.iterators.html
18.23
KB
-rw-r--r--
email.message.html
99.75
KB
-rw-r--r--
email.mime.html
44.89
KB
-rw-r--r--
email.parser.html
57.39
KB
-rw-r--r--
email.policy.html
84.24
KB
-rw-r--r--
email.utils.html
42.57
KB
-rw-r--r--
ensurepip.html
23.9
KB
-rw-r--r--
enum.html
151
KB
-rw-r--r--
errno.html
60.17
KB
-rw-r--r--
exceptions.html
108.44
KB
-rw-r--r--
faulthandler.html
31.5
KB
-rw-r--r--
fcntl.html
36.67
KB
-rw-r--r--
filecmp.html
32.08
KB
-rw-r--r--
fileformats.html
13.52
KB
-rw-r--r--
fileinput.html
40.37
KB
-rw-r--r--
filesys.html
16.78
KB
-rw-r--r--
fnmatch.html
21.02
KB
-rw-r--r--
fractions.html
35.29
KB
-rw-r--r--
frameworks.html
16.15
KB
-rw-r--r--
ftplib.html
71.97
KB
-rw-r--r--
functional.html
12.19
KB
-rw-r--r--
functions.html
260.33
KB
-rw-r--r--
functools.html
92.96
KB
-rw-r--r--
gc.html
40.97
KB
-rw-r--r--
getopt.html
29.4
KB
-rw-r--r--
getpass.html
16.01
KB
-rw-r--r--
gettext.html
98.02
KB
-rw-r--r--
glob.html
27.2
KB
-rw-r--r--
graphlib.html
34.82
KB
-rw-r--r--
grp.html
16.03
KB
-rw-r--r--
gzip.html
45.65
KB
-rw-r--r--
hashlib.html
93.19
KB
-rw-r--r--
heapq.html
42.43
KB
-rw-r--r--
hmac.html
24.74
KB
-rw-r--r--
html.entities.html
14.91
KB
-rw-r--r--
html.html
14.81
KB
-rw-r--r--
html.parser.html
47.7
KB
-rw-r--r--
http.client.html
85.63
KB
-rw-r--r--
http.cookiejar.html
113.94
KB
-rw-r--r--
http.cookies.html
47.64
KB
-rw-r--r--
http.html
45.52
KB
-rw-r--r--
http.server.html
73.05
KB
-rw-r--r--
i18n.html
13.44
KB
-rw-r--r--
idle.html
77.18
KB
-rw-r--r--
imaplib.html
87.08
KB
-rw-r--r--
imghdr.html
17.94
KB
-rw-r--r--
imp.html
54.32
KB
-rw-r--r--
importlib.html
239.96
KB
-rw-r--r--
importlib.metadata.html
43.14
KB
-rw-r--r--
index.html
75.06
KB
-rw-r--r--
inspect.html
168.74
KB
-rw-r--r--
internet.html
29.89
KB
-rw-r--r--
intro.html
13.81
KB
-rw-r--r--
io.html
154.15
KB
-rw-r--r--
ipaddress.html
137.75
KB
-rw-r--r--
ipc.html
12.6
KB
-rw-r--r--
itertools.html
153.27
KB
-rw-r--r--
json.html
98.9
KB
-rw-r--r--
keyword.html
14.88
KB
-rw-r--r--
language.html
16.71
KB
-rw-r--r--
linecache.html
18.09
KB
-rw-r--r--
locale.html
77.35
KB
-rw-r--r--
logging.config.html
100.3
KB
-rw-r--r--
logging.handlers.html
146.28
KB
-rw-r--r--
logging.html
169.67
KB
-rw-r--r--
lzma.html
66.89
KB
-rw-r--r--
mailbox.html
176.34
KB
-rw-r--r--
mailcap.html
20.86
KB
-rw-r--r--
markup.html
22.31
KB
-rw-r--r--
marshal.html
24.43
KB
-rw-r--r--
math.html
85.82
KB
-rw-r--r--
mimetypes.html
40.27
KB
-rw-r--r--
mm.html
11.16
KB
-rw-r--r--
mmap.html
55.53
KB
-rw-r--r--
modulefinder.html
23.67
KB
-rw-r--r--
modules.html
15.88
KB
-rw-r--r--
msilib.html
78.38
KB
-rw-r--r--
msvcrt.html
29.22
KB
-rw-r--r--
multiprocessing.html
405.24
KB
-rw-r--r--
multiprocessing.shared_memory....
60
KB
-rw-r--r--
netdata.html
20.24
KB
-rw-r--r--
netrc.html
20.39
KB
-rw-r--r--
nis.html
16.76
KB
-rw-r--r--
nntplib.html
84.93
KB
-rw-r--r--
numbers.html
45.67
KB
-rw-r--r--
numeric.html
19.29
KB
-rw-r--r--
operator.html
107.32
KB
-rw-r--r--
optparse.html
256.96
KB
-rw-r--r--
os.html
594.39
KB
-rw-r--r--
os.path.html
65.54
KB
-rw-r--r--
ossaudiodev.html
56.35
KB
-rw-r--r--
pathlib.html
170.01
KB
-rw-r--r--
pdb.html
68.67
KB
-rw-r--r--
persistence.html
19.13
KB
-rw-r--r--
pickle.html
154.45
KB
-rw-r--r--
pickletools.html
22.92
KB
-rw-r--r--
pipes.html
21.7
KB
-rw-r--r--
pkgutil.html
42.76
KB
-rw-r--r--
platform.html
45.08
KB
-rw-r--r--
plistlib.html
30.06
KB
-rw-r--r--
poplib.html
41.42
KB
-rw-r--r--
posix.html
20.96
KB
-rw-r--r--
pprint.html
55.35
KB
-rw-r--r--
profile.html
84.63
KB
-rw-r--r--
pty.html
23.7
KB
-rw-r--r--
pwd.html
16.84
KB
-rw-r--r--
py_compile.html
29
KB
-rw-r--r--
pyclbr.html
24.06
KB
-rw-r--r--
pydoc.html
19.59
KB
-rw-r--r--
pyexpat.html
104.38
KB
-rw-r--r--
python.html
22.46
KB
-rw-r--r--
queue.html
43.39
KB
-rw-r--r--
quopri.html
18.64
KB
-rw-r--r--
random.html
86.31
KB
-rw-r--r--
re.html
214.24
KB
-rw-r--r--
readline.html
50.19
KB
-rw-r--r--
reprlib.html
30.85
KB
-rw-r--r--
resource.html
48.26
KB
-rw-r--r--
rlcompleter.html
18.06
KB
-rw-r--r--
runpy.html
30.99
KB
-rw-r--r--
sched.html
27.04
KB
-rw-r--r--
secrets.html
30.2
KB
-rw-r--r--
security_warnings.html
16.21
KB
-rw-r--r--
select.html
74.58
KB
-rw-r--r--
selectors.html
41.91
KB
-rw-r--r--
shelve.html
36.54
KB
-rw-r--r--
shlex.html
61.03
KB
-rw-r--r--
shutil.html
100.13
KB
-rw-r--r--
signal.html
95.39
KB
-rw-r--r--
site.html
38.16
KB
-rw-r--r--
smtpd.html
44.09
KB
-rw-r--r--
smtplib.html
81.4
KB
-rw-r--r--
sndhdr.html
16.4
KB
-rw-r--r--
socket.html
256.95
KB
-rw-r--r--
socketserver.html
91.95
KB
-rw-r--r--
spwd.html
16.71
KB
-rw-r--r--
sqlite3.html
225.51
KB
-rw-r--r--
ssl.html
328.83
KB
-rw-r--r--
stat.html
57.79
KB
-rw-r--r--
statistics.html
123.22
KB
-rw-r--r--
stdtypes.html
615.43
KB
-rw-r--r--
string.html
108.03
KB
-rw-r--r--
stringprep.html
24.94
KB
-rw-r--r--
struct.html
70.58
KB
-rw-r--r--
subprocess.html
199.03
KB
-rw-r--r--
sunau.html
39.84
KB
-rw-r--r--
superseded.html
26.21
KB
-rw-r--r--
symtable.html
33.26
KB
-rw-r--r--
sys.html
198.83
KB
-rw-r--r--
sysconfig.html
39.81
KB
-rw-r--r--
syslog.html
27.36
KB
-rw-r--r--
tabnanny.html
16.18
KB
-rw-r--r--
tarfile.html
161.92
KB
-rw-r--r--
telnetlib.html
38.21
KB
-rw-r--r--
tempfile.html
58.39
KB
-rw-r--r--
termios.html
22.86
KB
-rw-r--r--
test.html
222.85
KB
-rw-r--r--
text.html
16.8
KB
-rw-r--r--
textwrap.html
48.83
KB
-rw-r--r--
threading.html
128.33
KB
-rw-r--r--
time.html
107.46
KB
-rw-r--r--
timeit.html
52.63
KB
-rw-r--r--
tk.html
28.08
KB
-rw-r--r--
tkinter.colorchooser.html
14.49
KB
-rw-r--r--
tkinter.dnd.html
17.09
KB
-rw-r--r--
tkinter.font.html
22.21
KB
-rw-r--r--
tkinter.html
106.85
KB
-rw-r--r--
tkinter.messagebox.html
20.09
KB
-rw-r--r--
tkinter.scrolledtext.html
14.52
KB
-rw-r--r--
tkinter.tix.html
59.69
KB
-rw-r--r--
tkinter.ttk.html
138.37
KB
-rw-r--r--
token.html
45.73
KB
-rw-r--r--
tokenize.html
39.86
KB
-rw-r--r--
trace.html
38.85
KB
-rw-r--r--
traceback.html
75.61
KB
-rw-r--r--
tracemalloc.html
122.33
KB
-rw-r--r--
tty.html
14.46
KB
-rw-r--r--
turtle.html
271.71
KB
-rw-r--r--
types.html
64.85
KB
-rw-r--r--
typing.html
315.69
KB
-rw-r--r--
unicodedata.html
27.3
KB
-rw-r--r--
unittest.html
310.01
KB
-rw-r--r--
unittest.mock-examples.html
177.75
KB
-rw-r--r--
unittest.mock.html
363.51
KB
-rw-r--r--
unix.html
13.46
KB
-rw-r--r--
urllib.error.html
18.56
KB
-rw-r--r--
urllib.html
12.92
KB
-rw-r--r--
urllib.parse.html
104.65
KB
-rw-r--r--
urllib.request.html
208.87
KB
-rw-r--r--
urllib.robotparser.html
22.63
KB
-rw-r--r--
uu.html
18.2
KB
-rw-r--r--
uuid.html
42.88
KB
-rw-r--r--
venv.html
90.43
KB
-rw-r--r--
warnings.html
69.13
KB
-rw-r--r--
wave.html
36.12
KB
-rw-r--r--
weakref.html
73.36
KB
-rw-r--r--
webbrowser.html
33.98
KB
-rw-r--r--
windows.html
12.32
KB
-rw-r--r--
winreg.html
89.48
KB
-rw-r--r--
winsound.html
26.3
KB
-rw-r--r--
wsgiref.html
111.52
KB
-rw-r--r--
xdrlib.html
42.07
KB
-rw-r--r--
xml.dom.html
114.66
KB
-rw-r--r--
xml.dom.minidom.html
49.6
KB
-rw-r--r--
xml.dom.pulldom.html
30.06
KB
-rw-r--r--
xml.etree.elementtree.html
186.49
KB
-rw-r--r--
xml.html
22.34
KB
-rw-r--r--
xml.sax.handler.html
55.78
KB
-rw-r--r--
xml.sax.html
31.74
KB
-rw-r--r--
xml.sax.reader.html
56.23
KB
-rw-r--r--
xml.sax.utils.html
22.28
KB
-rw-r--r--
xmlrpc.client.html
79.73
KB
-rw-r--r--
xmlrpc.html
11.9
KB
-rw-r--r--
xmlrpc.server.html
63.56
KB
-rw-r--r--
zipapp.html
52.45
KB
-rw-r--r--
zipfile.html
116.59
KB
-rw-r--r--
zipimport.html
33.76
KB
-rw-r--r--
zlib.html
42.29
KB
-rw-r--r--
zoneinfo.html
57.25
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : tracemalloc.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>tracemalloc — Trace memory allocations — Python 3.10.12 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?2022.1" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.10.12 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="Software Packaging and Distribution" href="distribution.html" /> <link rel="prev" title="trace — Trace or track Python statement execution" href="trace.html" /> <link rel="canonical" href="file:///usr/share/doc/python3.10/html/library/tracemalloc.html" /> <style> @media only screen { table.full-width-table { width: 100%; } } </style> <link rel="shortcut icon" type="image/png" href="../_static/py.svg" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/menu.js"></script> </head> <body> <div class="mobile-nav"> <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation" aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" /> <label for="menuToggler" class="toggler__label"> <span></span> </label> <nav class="nav-content" role="navigation"> <a href="https://www.python.org/" class="nav-logo"> <img src="../_static/py.svg" alt="Logo"/> </a> <div class="version_switcher_placeholder"></div> <form role="search" class="search" action="../search.html" method="get"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon"> <path fill-rule="nonzero" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path> </svg> <input type="text" name="q" aria-label="Quick search"/> <input type="submit" value="Go"/> </form> </nav> <div class="menu-wrapper"> <nav class="menu" role="navigation" aria-label="main navigation"> <div class="language_switcher_placeholder"></div> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a><ul> <li><a class="reference internal" href="#examples">Examples</a><ul> <li><a class="reference internal" href="#display-the-top-10">Display the top 10</a></li> <li><a class="reference internal" href="#compute-differences">Compute differences</a></li> <li><a class="reference internal" href="#get-the-traceback-of-a-memory-block">Get the traceback of a memory block</a></li> <li><a class="reference internal" href="#pretty-top">Pretty top</a><ul> <li><a class="reference internal" href="#record-the-current-and-peak-size-of-all-traced-memory-blocks">Record the current and peak size of all traced memory blocks</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#api">API</a><ul> <li><a class="reference internal" href="#functions">Functions</a></li> <li><a class="reference internal" href="#domainfilter">DomainFilter</a></li> <li><a class="reference internal" href="#filter">Filter</a></li> <li><a class="reference internal" href="#frame">Frame</a></li> <li><a class="reference internal" href="#snapshot">Snapshot</a></li> <li><a class="reference internal" href="#statistic">Statistic</a></li> <li><a class="reference internal" href="#statisticdiff">StatisticDiff</a></li> <li><a class="reference internal" href="#trace">Trace</a></li> <li><a class="reference internal" href="#traceback">Traceback</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="trace.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a></p> <h4>Next topic</h4> <p class="topless"><a href="distribution.html" title="next chapter">Software Packaging and Distribution</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/tracemalloc.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </nav> </div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="distribution.html" title="Software Packaging and Distribution" accesskey="N">next</a> |</li> <li class="right" > <a href="trace.html" title="trace — Trace or track Python statement execution" accesskey="P">previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="debug.html" accesskey="U">Debugging and Profiling</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-tracemalloc"> <span id="tracemalloc-trace-memory-allocations"></span><h1><a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> — Trace memory allocations<a class="headerlink" href="#module-tracemalloc" title="Permalink to this headline">¶</a></h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.10/Lib/tracemalloc.py">Lib/tracemalloc.py</a></p> <hr class="docutils" /> <p>The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides the following information:</p> <ul class="simple"> <li><p>Traceback where an object was allocated</p></li> <li><p>Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks</p></li> <li><p>Compute the differences between two snapshots to detect memory leaks</p></li> </ul> <p>To trace most memory blocks allocated by Python, the module should be started as early as possible by setting the <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC</span></code></a> environment variable to <code class="docutils literal notranslate"><span class="pre">1</span></code>, or by using <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code> command line option. The <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a> function can be called at runtime to start tracing Python memory allocations.</p> <p>By default, a trace of an allocated memory block only stores the most recent frame (1 frame). To store 25 frames at startup: set the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC</span></code></a> environment variable to <code class="docutils literal notranslate"><span class="pre">25</span></code>, or use the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">tracemalloc=25</span></code> command line option.</p> <section id="examples"> <h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2> <section id="display-the-top-10"> <h3>Display the top 10<a class="headerlink" href="#display-the-top-10" title="Permalink to this headline">¶</a></h3> <p>Display the 10 files allocating the most memory:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tracemalloc</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c1"># ... run your application ...</span> <span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span> <span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="s1">'lineno'</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"[ Top 10 ]"</span><span class="p">)</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">[:</span><span class="mi">10</span><span class="p">]:</span> <span class="nb">print</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span> </pre></div> </div> <p>Example of output of the Python test suite:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span> <span class="n">Top</span> <span class="mi">10</span> <span class="p">]</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">716</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">4855</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">39328</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">126</span> <span class="n">B</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">284</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">521</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">3199</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">167</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">collections</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">368</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">244</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">2315</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">108</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">381</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">185</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">779</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">243</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">402</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">154</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">378</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">416</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">abc</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">133</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">88.7</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">347</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">262</span> <span class="n">B</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">1446</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">70.4</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">911</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">79</span> <span class="n">B</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">1454</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">52.0</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">2131</span> <span class="n">B</span> <span class="o"><</span><span class="n">string</span><span class="o">></span><span class="p">:</span><span class="mi">5</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">49.7</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">148</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mi">344</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">sysconfig</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">411</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">48.0</span> <span class="n">KiB</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="mf">48.0</span> <span class="n">KiB</span> </pre></div> </div> <p>We can see that Python loaded <code class="docutils literal notranslate"><span class="pre">4855</span> <span class="pre">KiB</span></code> data (bytecode and constants) from modules and that the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module allocated <code class="docutils literal notranslate"><span class="pre">244</span> <span class="pre">KiB</span></code> to build <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">namedtuple</span></code></a> types.</p> <p>See <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> for more options.</p> </section> <section id="compute-differences"> <h3>Compute differences<a class="headerlink" href="#compute-differences" title="Permalink to this headline">¶</a></h3> <p>Take two snapshots and display the differences:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tracemalloc</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c1"># ... start your application ...</span> <span class="n">snapshot1</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span> <span class="c1"># ... call the function leaking memory ...</span> <span class="n">snapshot2</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span> <span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot2</span><span class="o">.</span><span class="n">compare_to</span><span class="p">(</span><span class="n">snapshot1</span><span class="p">,</span> <span class="s1">'lineno'</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"[ Top 10 differences ]"</span><span class="p">)</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">[:</span><span class="mi">10</span><span class="p">]:</span> <span class="nb">print</span><span class="p">(</span><span class="n">stat</span><span class="p">)</span> </pre></div> </div> <p>Example of output before/after running some tests of the Python test suite:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span> <span class="n">Top</span> <span class="mi">10</span> <span class="n">differences</span> <span class="p">]</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">716</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">8173</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">4428</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">71332</span> <span class="p">(</span><span class="o">+</span><span class="mi">39369</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">117</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">linecache</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">127</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">940</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">940</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">8106</span> <span class="p">(</span><span class="o">+</span><span class="mi">8106</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">119</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">case</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">571</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">298</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">298</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">589</span> <span class="p">(</span><span class="o">+</span><span class="mi">589</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">519</span> <span class="n">B</span> <span class="o"><</span><span class="n">frozen</span> <span class="n">importlib</span><span class="o">.</span><span class="n">_bootstrap</span><span class="o">></span><span class="p">:</span><span class="mi">284</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">1005</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">166</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">7423</span> <span class="p">(</span><span class="o">+</span><span class="mi">1526</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">139</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">mimetypes</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">217</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mi">112</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mi">112</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">1334</span> <span class="p">(</span><span class="o">+</span><span class="mi">1334</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">86</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">http</span><span class="o">/</span><span class="n">server</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">848</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">96.0</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">96.0</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span> <span class="p">(</span><span class="o">+</span><span class="mi">1</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mf">96.0</span> <span class="n">KiB</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">inspect</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">1465</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">83.5</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">83.5</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">109</span> <span class="p">(</span><span class="o">+</span><span class="mi">109</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">784</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">unittest</span><span class="o">/</span><span class="n">mock</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">491</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">77.7</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">77.7</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">143</span> <span class="p">(</span><span class="o">+</span><span class="mi">143</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">557</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">urllib</span><span class="o">/</span><span class="n">parse</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">476</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">71.8</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">71.8</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">969</span> <span class="p">(</span><span class="o">+</span><span class="mi">969</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">76</span> <span class="n">B</span> <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python3</span><span class="mf">.4</span><span class="o">/</span><span class="n">contextlib</span><span class="o">.</span><span class="n">py</span><span class="p">:</span><span class="mi">38</span><span class="p">:</span> <span class="n">size</span><span class="o">=</span><span class="mf">67.2</span> <span class="n">KiB</span> <span class="p">(</span><span class="o">+</span><span class="mf">67.2</span> <span class="n">KiB</span><span class="p">),</span> <span class="n">count</span><span class="o">=</span><span class="mi">126</span> <span class="p">(</span><span class="o">+</span><span class="mi">126</span><span class="p">),</span> <span class="n">average</span><span class="o">=</span><span class="mi">546</span> <span class="n">B</span> </pre></div> </div> <p>We can see that Python has loaded <code class="docutils literal notranslate"><span class="pre">8173</span> <span class="pre">KiB</span></code> of module data (bytecode and constants), and that this is <code class="docutils literal notranslate"><span class="pre">4428</span> <span class="pre">KiB</span></code> more than had been loaded before the tests, when the previous snapshot was taken. Similarly, the <a class="reference internal" href="linecache.html#module-linecache" title="linecache: Provides random access to individual lines from text files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code></a> module has cached <code class="docutils literal notranslate"><span class="pre">940</span> <span class="pre">KiB</span></code> of Python source code to format tracebacks, all of it since the previous snapshot.</p> <p>If the system has little free memory, snapshots can be written on disk using the <a class="reference internal" href="#tracemalloc.Snapshot.dump" title="tracemalloc.Snapshot.dump"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.dump()</span></code></a> method to analyze the snapshot offline. Then use the <a class="reference internal" href="#tracemalloc.Snapshot.load" title="tracemalloc.Snapshot.load"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.load()</span></code></a> method reload the snapshot.</p> </section> <section id="get-the-traceback-of-a-memory-block"> <h3>Get the traceback of a memory block<a class="headerlink" href="#get-the-traceback-of-a-memory-block" title="Permalink to this headline">¶</a></h3> <p>Code to display the traceback of the biggest memory block:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tracemalloc</span> <span class="c1"># Store 25 frames</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span> <span class="c1"># ... run your application ...</span> <span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span> <span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="s1">'traceback'</span><span class="p">)</span> <span class="c1"># pick the biggest memory block</span> <span class="n">stat</span> <span class="o">=</span> <span class="n">top_stats</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> memory blocks: </span><span class="si">%.1f</span><span class="s2"> KiB"</span> <span class="o">%</span> <span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">count</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">stat</span><span class="o">.</span><span class="n">traceback</span><span class="o">.</span><span class="n">format</span><span class="p">():</span> <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> </pre></div> </div> <p>Example of output of the Python test suite (traceback limited to 25 frames):</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">903</span> <span class="n">memory</span> <span class="n">blocks</span><span class="p">:</span> <span class="mf">870.1</span> <span class="n">KiB</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">716</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1036</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">934</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1068</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">619</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1581</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1614</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/doctest.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">101</span> <span class="kn">import</span> <span class="nn">pdb</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">284</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">938</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1068</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">619</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1581</span> <span class="n">File</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1614</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/support/__init__.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1728</span> <span class="kn">import</span> <span class="nn">doctest</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/test_pickletools.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">21</span> <span class="n">support</span><span class="o">.</span><span class="n">run_doctest</span><span class="p">(</span><span class="n">pickletools</span><span class="p">)</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/regrtest.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1276</span> <span class="n">test_runner</span><span class="p">()</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/regrtest.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">976</span> <span class="n">display_failure</span><span class="o">=</span><span class="ow">not</span> <span class="n">verbose</span><span class="p">)</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/regrtest.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">761</span> <span class="n">match_tests</span><span class="o">=</span><span class="n">ns</span><span class="o">.</span><span class="n">match_tests</span><span class="p">)</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/regrtest.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1563</span> <span class="n">main</span><span class="p">()</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/test/__main__.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">3</span> <span class="n">regrtest</span><span class="o">.</span><span class="n">main_in_temp_cwd</span><span class="p">()</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/runpy.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">73</span> <span class="n">exec</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">run_globals</span><span class="p">)</span> <span class="n">File</span> <span class="s2">"/usr/lib/python3.4/runpy.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">160</span> <span class="s2">"__main__"</span><span class="p">,</span> <span class="n">fname</span><span class="p">,</span> <span class="n">loader</span><span class="p">,</span> <span class="n">pkg_name</span><span class="p">)</span> </pre></div> </div> <p>We can see that the most memory was allocated in the <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> module to load data (bytecode and constants) from modules: <code class="docutils literal notranslate"><span class="pre">870.1</span> <span class="pre">KiB</span></code>. The traceback is where the <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> loaded data most recently: on the <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">pdb</span></code> line of the <a class="reference internal" href="doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module. The traceback may change if a new module is loaded.</p> </section> <section id="pretty-top"> <h3>Pretty top<a class="headerlink" href="#pretty-top" title="Permalink to this headline">¶</a></h3> <p>Code to display the 10 lines allocating the most memory with a pretty output, ignoring <code class="docutils literal notranslate"><span class="pre"><frozen</span> <span class="pre">importlib._bootstrap></span></code> and <code class="docutils literal notranslate"><span class="pre"><unknown></span></code> files:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">linecache</span> <span class="kn">import</span> <span class="nn">os</span> <span class="kn">import</span> <span class="nn">tracemalloc</span> <span class="k">def</span> <span class="nf">display_top</span><span class="p">(</span><span class="n">snapshot</span><span class="p">,</span> <span class="n">key_type</span><span class="o">=</span><span class="s1">'lineno'</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span> <span class="n">snapshot</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">filter_traces</span><span class="p">((</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">Filter</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="s2">"<frozen importlib._bootstrap>"</span><span class="p">),</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">Filter</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="s2">"<unknown>"</span><span class="p">),</span> <span class="p">))</span> <span class="n">top_stats</span> <span class="o">=</span> <span class="n">snapshot</span><span class="o">.</span><span class="n">statistics</span><span class="p">(</span><span class="n">key_type</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Top </span><span class="si">%s</span><span class="s2"> lines"</span> <span class="o">%</span> <span class="n">limit</span><span class="p">)</span> <span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">stat</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">top_stats</span><span class="p">[:</span><span class="n">limit</span><span class="p">],</span> <span class="mi">1</span><span class="p">):</span> <span class="n">frame</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">traceback</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"#</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">:</span><span class="si">%s</span><span class="s2">: </span><span class="si">%.1f</span><span class="s2"> KiB"</span> <span class="o">%</span> <span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">lineno</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span> <span class="n">line</span> <span class="o">=</span> <span class="n">linecache</span><span class="o">.</span><span class="n">getline</span><span class="p">(</span><span class="n">frame</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="n">frame</span><span class="o">.</span><span class="n">lineno</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">if</span> <span class="n">line</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s1">' </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span> <span class="n">other</span> <span class="o">=</span> <span class="n">top_stats</span><span class="p">[</span><span class="n">limit</span><span class="p">:]</span> <span class="k">if</span> <span class="n">other</span><span class="p">:</span> <span class="n">size</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">other</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">%s</span><span class="s2"> other: </span><span class="si">%.1f</span><span class="s2"> KiB"</span> <span class="o">%</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">other</span><span class="p">),</span> <span class="n">size</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span> <span class="n">total</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">size</span> <span class="k">for</span> <span class="n">stat</span> <span class="ow">in</span> <span class="n">top_stats</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"Total allocated size: </span><span class="si">%.1f</span><span class="s2"> KiB"</span> <span class="o">%</span> <span class="p">(</span><span class="n">total</span> <span class="o">/</span> <span class="mi">1024</span><span class="p">))</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c1"># ... run your application ...</span> <span class="n">snapshot</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">take_snapshot</span><span class="p">()</span> <span class="n">display_top</span><span class="p">(</span><span class="n">snapshot</span><span class="p">)</span> </pre></div> </div> <p>Example of output of the Python test suite:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Top</span> <span class="mi">10</span> <span class="n">lines</span> <span class="c1">#1: Lib/base64.py:414: 419.8 KiB</span> <span class="n">_b85chars2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_b85chars</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_b85chars</span><span class="p">]</span> <span class="c1">#2: Lib/base64.py:306: 419.8 KiB</span> <span class="n">_a85chars2</span> <span class="o">=</span> <span class="p">[(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_a85chars</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_a85chars</span><span class="p">]</span> <span class="c1">#3: collections/__init__.py:368: 293.6 KiB</span> <span class="n">exec</span><span class="p">(</span><span class="n">class_definition</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span> <span class="c1">#4: Lib/abc.py:133: 115.2 KiB</span> <span class="bp">cls</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__new__</span><span class="p">(</span><span class="n">mcls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">bases</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span> <span class="c1">#5: unittest/case.py:574: 103.1 KiB</span> <span class="n">testMethod</span><span class="p">()</span> <span class="c1">#6: Lib/linecache.py:127: 95.4 KiB</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span> <span class="c1">#7: urllib/parse.py:476: 71.8 KiB</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_hexdig</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_hexdig</span><span class="p">}</span> <span class="c1">#8: <string>:5: 62.0 KiB</span> <span class="c1">#9: Lib/_weakrefset.py:37: 60.0 KiB</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> <span class="c1">#10: Lib/base64.py:142: 59.8 KiB</span> <span class="n">_b32tab2</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">_b32tab</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">_b32tab</span><span class="p">]</span> <span class="mi">6220</span> <span class="n">other</span><span class="p">:</span> <span class="mf">3602.8</span> <span class="n">KiB</span> <span class="n">Total</span> <span class="n">allocated</span> <span class="n">size</span><span class="p">:</span> <span class="mf">5303.1</span> <span class="n">KiB</span> </pre></div> </div> <p>See <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> for more options.</p> <section id="record-the-current-and-peak-size-of-all-traced-memory-blocks"> <h4>Record the current and peak size of all traced memory blocks<a class="headerlink" href="#record-the-current-and-peak-size-of-all-traced-memory-blocks" title="Permalink to this headline">¶</a></h4> <p>The following code computes two sums like <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">+</span> <span class="pre">1</span> <span class="pre">+</span> <span class="pre">2</span> <span class="pre">+</span> <span class="pre">...</span></code> inefficiently, by creating a list of those numbers. This list consumes a lot of memory temporarily. We can use <a class="reference internal" href="#tracemalloc.get_traced_memory" title="tracemalloc.get_traced_memory"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traced_memory()</span></code></a> and <a class="reference internal" href="#tracemalloc.reset_peak" title="tracemalloc.reset_peak"><code class="xref py py-func docutils literal notranslate"><span class="pre">reset_peak()</span></code></a> to observe the small memory usage after the sum is computed as well as the peak memory usage during the computations:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tracemalloc</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c1"># Example code: compute a sum with a large temporary list</span> <span class="n">large_sum</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">100000</span><span class="p">)))</span> <span class="n">first_size</span><span class="p">,</span> <span class="n">first_peak</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">get_traced_memory</span><span class="p">()</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">reset_peak</span><span class="p">()</span> <span class="c1"># Example code: compute a sum with a small temporary list</span> <span class="n">small_sum</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">)))</span> <span class="n">second_size</span><span class="p">,</span> <span class="n">second_peak</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">get_traced_memory</span><span class="p">()</span> <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">first_size</span><span class="si">=}</span><span class="s2">, </span><span class="si">{</span><span class="n">first_peak</span><span class="si">=}</span><span class="s2">"</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">second_size</span><span class="si">=}</span><span class="s2">, </span><span class="si">{</span><span class="n">second_peak</span><span class="si">=}</span><span class="s2">"</span><span class="p">)</span> </pre></div> </div> <p>Output:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">first_size</span><span class="o">=</span><span class="mi">664</span><span class="p">,</span> <span class="n">first_peak</span><span class="o">=</span><span class="mi">3592984</span> <span class="n">second_size</span><span class="o">=</span><span class="mi">804</span><span class="p">,</span> <span class="n">second_peak</span><span class="o">=</span><span class="mi">29704</span> </pre></div> </div> <p>Using <a class="reference internal" href="#tracemalloc.reset_peak" title="tracemalloc.reset_peak"><code class="xref py py-func docutils literal notranslate"><span class="pre">reset_peak()</span></code></a> ensured we could accurately record the peak during the computation of <code class="docutils literal notranslate"><span class="pre">small_sum</span></code>, even though it is much smaller than the overall peak size of memory blocks since the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a> call. Without the call to <a class="reference internal" href="#tracemalloc.reset_peak" title="tracemalloc.reset_peak"><code class="xref py py-func docutils literal notranslate"><span class="pre">reset_peak()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">second_peak</span></code> would still be the peak from the computation <code class="docutils literal notranslate"><span class="pre">large_sum</span></code> (that is, equal to <code class="docutils literal notranslate"><span class="pre">first_peak</span></code>). In this case, both peaks are much higher than the final memory usage, and which suggests we could optimise (by removing the unnecessary call to <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>, and writing <code class="docutils literal notranslate"><span class="pre">sum(range(...))</span></code>).</p> </section> </section> </section> <section id="api"> <h2>API<a class="headerlink" href="#api" title="Permalink to this headline">¶</a></h2> <section id="functions"> <h3>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.clear_traces"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">clear_traces</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.clear_traces" title="Permalink to this definition">¶</a></dt> <dd><p>Clear traces of memory blocks allocated by Python.</p> <p>See also <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><code class="xref py py-func docutils literal notranslate"><span class="pre">stop()</span></code></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.get_object_traceback"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">get_object_traceback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.get_object_traceback" title="Permalink to this definition">¶</a></dt> <dd><p>Get the traceback where the Python object <em>obj</em> was allocated. Return a <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> instance, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module is not tracing memory allocations or did not trace the allocation of the object.</p> <p>See also <a class="reference internal" href="gc.html#gc.get_referrers" title="gc.get_referrers"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.get_referrers()</span></code></a> and <a class="reference internal" href="sys.html#sys.getsizeof" title="sys.getsizeof"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getsizeof()</span></code></a> functions.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.get_traceback_limit"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">get_traceback_limit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.get_traceback_limit" title="Permalink to this definition">¶</a></dt> <dd><p>Get the maximum number of frames stored in the traceback of a trace.</p> <p>The <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module must be tracing memory allocations to get the limit, otherwise an exception is raised.</p> <p>The limit is set by the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a> function.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.get_traced_memory"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">get_traced_memory</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.get_traced_memory" title="Permalink to this definition">¶</a></dt> <dd><p>Get the current size and peak size of memory blocks traced by the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module as a tuple: <code class="docutils literal notranslate"><span class="pre">(current:</span> <span class="pre">int,</span> <span class="pre">peak:</span> <span class="pre">int)</span></code>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.reset_peak"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">reset_peak</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.reset_peak" title="Permalink to this definition">¶</a></dt> <dd><p>Set the peak size of memory blocks traced by the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module to the current size.</p> <p>Do nothing if the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module is not tracing memory allocations.</p> <p>This function only modifies the recorded peak size, and does not modify or clear any traces, unlike <a class="reference internal" href="#tracemalloc.clear_traces" title="tracemalloc.clear_traces"><code class="xref py py-func docutils literal notranslate"><span class="pre">clear_traces()</span></code></a>. Snapshots taken with <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><code class="xref py py-func docutils literal notranslate"><span class="pre">take_snapshot()</span></code></a> before a call to <a class="reference internal" href="#tracemalloc.reset_peak" title="tracemalloc.reset_peak"><code class="xref py py-func docutils literal notranslate"><span class="pre">reset_peak()</span></code></a> can be meaningfully compared to snapshots taken after the call.</p> <p>See also <a class="reference internal" href="#tracemalloc.get_traced_memory" title="tracemalloc.get_traced_memory"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traced_memory()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.get_tracemalloc_memory"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">get_tracemalloc_memory</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.get_tracemalloc_memory" title="Permalink to this definition">¶</a></dt> <dd><p>Get the memory usage in bytes of the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module used to store traces of memory blocks. Return an <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.is_tracing"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">is_tracing</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.is_tracing" title="Permalink to this definition">¶</a></dt> <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module is tracing Python memory allocations, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p> <p>See also <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a> and <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><code class="xref py py-func docutils literal notranslate"><span class="pre">stop()</span></code></a> functions.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.start"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nframe</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.start" title="Permalink to this definition">¶</a></dt> <dd><p>Start tracing Python memory allocations: install hooks on Python memory allocators. Collected tracebacks of traces will be limited to <em>nframe</em> frames. By default, a trace of a memory block only stores the most recent frame: the limit is <code class="docutils literal notranslate"><span class="pre">1</span></code>. <em>nframe</em> must be greater or equal to <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p> <p>You can still read the original number of total frames that composed the traceback by looking at the <a class="reference internal" href="#tracemalloc.Traceback.total_nframe" title="tracemalloc.Traceback.total_nframe"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Traceback.total_nframe</span></code></a> attribute.</p> <p>Storing more than <code class="docutils literal notranslate"><span class="pre">1</span></code> frame is only useful to compute statistics grouped by <code class="docutils literal notranslate"><span class="pre">'traceback'</span></code> or to compute cumulative statistics: see the <a class="reference internal" href="#tracemalloc.Snapshot.compare_to" title="tracemalloc.Snapshot.compare_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.compare_to()</span></code></a> and <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> methods.</p> <p>Storing more frames increases the memory and CPU overhead of the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module. Use the <a class="reference internal" href="#tracemalloc.get_tracemalloc_memory" title="tracemalloc.get_tracemalloc_memory"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_tracemalloc_memory()</span></code></a> function to measure how much memory is used by the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module.</p> <p>The <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONTRACEMALLOC"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC</span></code></a> environment variable (<code class="docutils literal notranslate"><span class="pre">PYTHONTRACEMALLOC=NFRAME</span></code>) and the <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">tracemalloc=NFRAME</span></code> command line option can be used to start tracing at startup.</p> <p>See also <a class="reference internal" href="#tracemalloc.stop" title="tracemalloc.stop"><code class="xref py py-func docutils literal notranslate"><span class="pre">stop()</span></code></a>, <a class="reference internal" href="#tracemalloc.is_tracing" title="tracemalloc.is_tracing"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_tracing()</span></code></a> and <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traceback_limit()</span></code></a> functions.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.stop"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">stop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.stop" title="Permalink to this definition">¶</a></dt> <dd><p>Stop tracing Python memory allocations: uninstall hooks on Python memory allocators. Also clears all previously collected traces of memory blocks allocated by Python.</p> <p>Call <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><code class="xref py py-func docutils literal notranslate"><span class="pre">take_snapshot()</span></code></a> function to take a snapshot of traces before clearing them.</p> <p>See also <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a>, <a class="reference internal" href="#tracemalloc.is_tracing" title="tracemalloc.is_tracing"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_tracing()</span></code></a> and <a class="reference internal" href="#tracemalloc.clear_traces" title="tracemalloc.clear_traces"><code class="xref py py-func docutils literal notranslate"><span class="pre">clear_traces()</span></code></a> functions.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="tracemalloc.take_snapshot"> <span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">take_snapshot</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.take_snapshot" title="Permalink to this definition">¶</a></dt> <dd><p>Take a snapshot of traces of memory blocks allocated by Python. Return a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><code class="xref py py-class docutils literal notranslate"><span class="pre">Snapshot</span></code></a> instance.</p> <p>The snapshot does not include memory blocks allocated before the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module started to trace memory allocations.</p> <p>Tracebacks of traces are limited to <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traceback_limit()</span></code></a> frames. Use the <em>nframe</em> parameter of the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a> function to store more frames.</p> <p>The <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module must be tracing memory allocations to take a snapshot, see the <a class="reference internal" href="#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">start()</span></code></a> function.</p> <p>See also the <a class="reference internal" href="#tracemalloc.get_object_traceback" title="tracemalloc.get_object_traceback"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_object_traceback()</span></code></a> function.</p> </dd></dl> </section> <section id="domainfilter"> <h3>DomainFilter<a class="headerlink" href="#domainfilter" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.DomainFilter"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">DomainFilter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">inclusive</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">domain</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.DomainFilter" title="Permalink to this definition">¶</a></dt> <dd><p>Filter traces of memory blocks by their address space (domain).</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.DomainFilter.inclusive"> <span class="sig-name descname"><span class="pre">inclusive</span></span><a class="headerlink" href="#tracemalloc.DomainFilter.inclusive" title="Permalink to this definition">¶</a></dt> <dd><p>If <em>inclusive</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (include), match memory blocks allocated in the address space <a class="reference internal" href="#tracemalloc.DomainFilter.domain" title="tracemalloc.DomainFilter.domain"><code class="xref py py-attr docutils literal notranslate"><span class="pre">domain</span></code></a>.</p> <p>If <em>inclusive</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (exclude), match memory blocks not allocated in the address space <a class="reference internal" href="#tracemalloc.DomainFilter.domain" title="tracemalloc.DomainFilter.domain"><code class="xref py py-attr docutils literal notranslate"><span class="pre">domain</span></code></a>.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.DomainFilter.domain"> <span class="sig-name descname"><span class="pre">domain</span></span><a class="headerlink" href="#tracemalloc.DomainFilter.domain" title="Permalink to this definition">¶</a></dt> <dd><p>Address space of a memory block (<code class="docutils literal notranslate"><span class="pre">int</span></code>). Read-only property.</p> </dd></dl> </dd></dl> </section> <section id="filter"> <h3>Filter<a class="headerlink" href="#filter" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Filter"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Filter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">inclusive</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename_pattern</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_frames</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">domain</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#int" title="int"><span class="pre">int</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Filter" title="Permalink to this definition">¶</a></dt> <dd><p>Filter on traces of memory blocks.</p> <p>See the <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch.fnmatch()</span></code></a> function for the syntax of <em>filename_pattern</em>. The <code class="docutils literal notranslate"><span class="pre">'.pyc'</span></code> file extension is replaced with <code class="docutils literal notranslate"><span class="pre">'.py'</span></code>.</p> <p>Examples:</p> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">Filter(True,</span> <span class="pre">subprocess.__file__)</span></code> only includes traces of the <a class="reference internal" href="subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">Filter(False,</span> <span class="pre">tracemalloc.__file__)</span></code> excludes traces of the <a class="reference internal" href="#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> module</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">Filter(False,</span> <span class="pre">"<unknown>")</span></code> excludes empty tracebacks</p></li> </ul> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="docutils literal notranslate"><span class="pre">'.pyo'</span></code> file extension is no longer replaced with <code class="docutils literal notranslate"><span class="pre">'.py'</span></code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <a class="reference internal" href="#tracemalloc.Filter.domain" title="tracemalloc.Filter.domain"><code class="xref py py-attr docutils literal notranslate"><span class="pre">domain</span></code></a> attribute.</p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Filter.domain"> <span class="sig-name descname"><span class="pre">domain</span></span><a class="headerlink" href="#tracemalloc.Filter.domain" title="Permalink to this definition">¶</a></dt> <dd><p>Address space of a memory block (<code class="docutils literal notranslate"><span class="pre">int</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p> <p>tracemalloc uses the domain <code class="docutils literal notranslate"><span class="pre">0</span></code> to trace memory allocations made by Python. C extensions can use other domains to trace other resources.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Filter.inclusive"> <span class="sig-name descname"><span class="pre">inclusive</span></span><a class="headerlink" href="#tracemalloc.Filter.inclusive" title="Permalink to this definition">¶</a></dt> <dd><p>If <em>inclusive</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (include), only match memory blocks allocated in a file with a name matching <a class="reference internal" href="#tracemalloc.Filter.filename_pattern" title="tracemalloc.Filter.filename_pattern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename_pattern</span></code></a> at line number <a class="reference internal" href="#tracemalloc.Filter.lineno" title="tracemalloc.Filter.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a>.</p> <p>If <em>inclusive</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (exclude), ignore memory blocks allocated in a file with a name matching <a class="reference internal" href="#tracemalloc.Filter.filename_pattern" title="tracemalloc.Filter.filename_pattern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename_pattern</span></code></a> at line number <a class="reference internal" href="#tracemalloc.Filter.lineno" title="tracemalloc.Filter.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a>.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Filter.lineno"> <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#tracemalloc.Filter.lineno" title="Permalink to this definition">¶</a></dt> <dd><p>Line number (<code class="docutils literal notranslate"><span class="pre">int</span></code>) of the filter. If <em>lineno</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the filter matches any line number.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Filter.filename_pattern"> <span class="sig-name descname"><span class="pre">filename_pattern</span></span><a class="headerlink" href="#tracemalloc.Filter.filename_pattern" title="Permalink to this definition">¶</a></dt> <dd><p>Filename pattern of the filter (<code class="docutils literal notranslate"><span class="pre">str</span></code>). Read-only property.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Filter.all_frames"> <span class="sig-name descname"><span class="pre">all_frames</span></span><a class="headerlink" href="#tracemalloc.Filter.all_frames" title="Permalink to this definition">¶</a></dt> <dd><p>If <em>all_frames</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, all frames of the traceback are checked. If <em>all_frames</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, only the most recent frame is checked.</p> <p>This attribute has no effect if the traceback limit is <code class="docutils literal notranslate"><span class="pre">1</span></code>. See the <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traceback_limit()</span></code></a> function and <a class="reference internal" href="#tracemalloc.Snapshot.traceback_limit" title="tracemalloc.Snapshot.traceback_limit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Snapshot.traceback_limit</span></code></a> attribute.</p> </dd></dl> </dd></dl> </section> <section id="frame"> <h3>Frame<a class="headerlink" href="#frame" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Frame"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Frame</span></span><a class="headerlink" href="#tracemalloc.Frame" title="Permalink to this definition">¶</a></dt> <dd><p>Frame of a traceback.</p> <p>The <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> class is a sequence of <a class="reference internal" href="#tracemalloc.Frame" title="tracemalloc.Frame"><code class="xref py py-class docutils literal notranslate"><span class="pre">Frame</span></code></a> instances.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Frame.filename"> <span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#tracemalloc.Frame.filename" title="Permalink to this definition">¶</a></dt> <dd><p>Filename (<code class="docutils literal notranslate"><span class="pre">str</span></code>).</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Frame.lineno"> <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#tracemalloc.Frame.lineno" title="Permalink to this definition">¶</a></dt> <dd><p>Line number (<code class="docutils literal notranslate"><span class="pre">int</span></code>).</p> </dd></dl> </dd></dl> </section> <section id="snapshot"> <h3>Snapshot<a class="headerlink" href="#snapshot" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Snapshot"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Snapshot</span></span><a class="headerlink" href="#tracemalloc.Snapshot" title="Permalink to this definition">¶</a></dt> <dd><p>Snapshot of traces of memory blocks allocated by Python.</p> <p>The <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><code class="xref py py-func docutils literal notranslate"><span class="pre">take_snapshot()</span></code></a> function creates a snapshot instance.</p> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.compare_to"> <span class="sig-name descname"><span class="pre">compare_to</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">old_snapshot</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><span class="pre">Snapshot</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">key_type</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">cumulative</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Snapshot.compare_to" title="Permalink to this definition">¶</a></dt> <dd><p>Compute the differences with an old snapshot. Get statistics as a sorted list of <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><code class="xref py py-class docutils literal notranslate"><span class="pre">StatisticDiff</span></code></a> instances grouped by <em>key_type</em>.</p> <p>See the <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> method for <em>key_type</em> and <em>cumulative</em> parameters.</p> <p>The result is sorted from the biggest to the smallest by: absolute value of <a class="reference internal" href="#tracemalloc.StatisticDiff.size_diff" title="tracemalloc.StatisticDiff.size_diff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StatisticDiff.size_diff</span></code></a>, <a class="reference internal" href="#tracemalloc.StatisticDiff.size" title="tracemalloc.StatisticDiff.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StatisticDiff.size</span></code></a>, absolute value of <a class="reference internal" href="#tracemalloc.StatisticDiff.count_diff" title="tracemalloc.StatisticDiff.count_diff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StatisticDiff.count_diff</span></code></a>, <a class="reference internal" href="#tracemalloc.Statistic.count" title="tracemalloc.Statistic.count"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Statistic.count</span></code></a> and then by <a class="reference internal" href="#tracemalloc.StatisticDiff.traceback" title="tracemalloc.StatisticDiff.traceback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">StatisticDiff.traceback</span></code></a>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.dump"> <span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Snapshot.dump" title="Permalink to this definition">¶</a></dt> <dd><p>Write the snapshot into a file.</p> <p>Use <a class="reference internal" href="#tracemalloc.Snapshot.load" title="tracemalloc.Snapshot.load"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load()</span></code></a> to reload the snapshot.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.filter_traces"> <span class="sig-name descname"><span class="pre">filter_traces</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filters</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Snapshot.filter_traces" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><code class="xref py py-class docutils literal notranslate"><span class="pre">Snapshot</span></code></a> instance with a filtered <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><code class="xref py py-attr docutils literal notranslate"><span class="pre">traces</span></code></a> sequence, <em>filters</em> is a list of <a class="reference internal" href="#tracemalloc.DomainFilter" title="tracemalloc.DomainFilter"><code class="xref py py-class docutils literal notranslate"><span class="pre">DomainFilter</span></code></a> and <a class="reference internal" href="#tracemalloc.Filter" title="tracemalloc.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code></a> instances. If <em>filters</em> is an empty list, return a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><code class="xref py py-class docutils literal notranslate"><span class="pre">Snapshot</span></code></a> instance with a copy of the traces.</p> <p>All inclusive filters are applied at once, a trace is ignored if no inclusive filters match it. A trace is ignored if at least one exclusive filter matches it.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#tracemalloc.DomainFilter" title="tracemalloc.DomainFilter"><code class="xref py py-class docutils literal notranslate"><span class="pre">DomainFilter</span></code></a> instances are now also accepted in <em>filters</em>.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.load"> <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">load</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Snapshot.load" title="Permalink to this definition">¶</a></dt> <dd><p>Load a snapshot from a file.</p> <p>See also <a class="reference internal" href="#tracemalloc.Snapshot.dump" title="tracemalloc.Snapshot.dump"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dump()</span></code></a>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.statistics"> <span class="sig-name descname"><span class="pre">statistics</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key_type</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="stdtypes.html#str" title="str"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">cumulative</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="functions.html#bool" title="bool"><span class="pre">bool</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Snapshot.statistics" title="Permalink to this definition">¶</a></dt> <dd><p>Get statistics as a sorted list of <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Statistic</span></code></a> instances grouped by <em>key_type</em>:</p> <table class="docutils align-default"> <colgroup> <col style="width: 47%" /> <col style="width: 53%" /> </colgroup> <thead> <tr class="row-odd"><th class="head"><p>key_type</p></th> <th class="head"><p>description</p></th> </tr> </thead> <tbody> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'filename'</span></code></p></td> <td><p>filename</p></td> </tr> <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'lineno'</span></code></p></td> <td><p>filename and line number</p></td> </tr> <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'traceback'</span></code></p></td> <td><p>traceback</p></td> </tr> </tbody> </table> <p>If <em>cumulative</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, cumulate size and count of memory blocks of all frames of the traceback of a trace, not only the most recent frame. The cumulative mode can only be used with <em>key_type</em> equals to <code class="docutils literal notranslate"><span class="pre">'filename'</span></code> and <code class="docutils literal notranslate"><span class="pre">'lineno'</span></code>.</p> <p>The result is sorted from the biggest to the smallest by: <a class="reference internal" href="#tracemalloc.Statistic.size" title="tracemalloc.Statistic.size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Statistic.size</span></code></a>, <a class="reference internal" href="#tracemalloc.Statistic.count" title="tracemalloc.Statistic.count"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Statistic.count</span></code></a> and then by <a class="reference internal" href="#tracemalloc.Statistic.traceback" title="tracemalloc.Statistic.traceback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Statistic.traceback</span></code></a>.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.traceback_limit"> <span class="sig-name descname"><span class="pre">traceback_limit</span></span><a class="headerlink" href="#tracemalloc.Snapshot.traceback_limit" title="Permalink to this definition">¶</a></dt> <dd><p>Maximum number of frames stored in the traceback of <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><code class="xref py py-attr docutils literal notranslate"><span class="pre">traces</span></code></a>: result of the <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traceback_limit()</span></code></a> when the snapshot was taken.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Snapshot.traces"> <span class="sig-name descname"><span class="pre">traces</span></span><a class="headerlink" href="#tracemalloc.Snapshot.traces" title="Permalink to this definition">¶</a></dt> <dd><p>Traces of all memory blocks allocated by Python: sequence of <a class="reference internal" href="#tracemalloc.Trace" title="tracemalloc.Trace"><code class="xref py py-class docutils literal notranslate"><span class="pre">Trace</span></code></a> instances.</p> <p>The sequence has an undefined order. Use the <a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> method to get a sorted list of statistics.</p> </dd></dl> </dd></dl> </section> <section id="statistic"> <h3>Statistic<a class="headerlink" href="#statistic" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Statistic"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Statistic</span></span><a class="headerlink" href="#tracemalloc.Statistic" title="Permalink to this definition">¶</a></dt> <dd><p>Statistic on memory allocations.</p> <p><a class="reference internal" href="#tracemalloc.Snapshot.statistics" title="tracemalloc.Snapshot.statistics"><code class="xref py py-func docutils literal notranslate"><span class="pre">Snapshot.statistics()</span></code></a> returns a list of <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Statistic</span></code></a> instances.</p> <p>See also the <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><code class="xref py py-class docutils literal notranslate"><span class="pre">StatisticDiff</span></code></a> class.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Statistic.count"> <span class="sig-name descname"><span class="pre">count</span></span><a class="headerlink" href="#tracemalloc.Statistic.count" title="Permalink to this definition">¶</a></dt> <dd><p>Number of memory blocks (<code class="docutils literal notranslate"><span class="pre">int</span></code>).</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Statistic.size"> <span class="sig-name descname"><span class="pre">size</span></span><a class="headerlink" href="#tracemalloc.Statistic.size" title="Permalink to this definition">¶</a></dt> <dd><p>Total size of memory blocks in bytes (<code class="docutils literal notranslate"><span class="pre">int</span></code>).</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Statistic.traceback"> <span class="sig-name descname"><span class="pre">traceback</span></span><a class="headerlink" href="#tracemalloc.Statistic.traceback" title="Permalink to this definition">¶</a></dt> <dd><p>Traceback where the memory block was allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> instance.</p> </dd></dl> </dd></dl> </section> <section id="statisticdiff"> <h3>StatisticDiff<a class="headerlink" href="#statisticdiff" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">StatisticDiff</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff" title="Permalink to this definition">¶</a></dt> <dd><p>Statistic difference on memory allocations between an old and a new <a class="reference internal" href="#tracemalloc.Snapshot" title="tracemalloc.Snapshot"><code class="xref py py-class docutils literal notranslate"><span class="pre">Snapshot</span></code></a> instance.</p> <p><a class="reference internal" href="#tracemalloc.Snapshot.compare_to" title="tracemalloc.Snapshot.compare_to"><code class="xref py py-func docutils literal notranslate"><span class="pre">Snapshot.compare_to()</span></code></a> returns a list of <a class="reference internal" href="#tracemalloc.StatisticDiff" title="tracemalloc.StatisticDiff"><code class="xref py py-class docutils literal notranslate"><span class="pre">StatisticDiff</span></code></a> instances. See also the <a class="reference internal" href="#tracemalloc.Statistic" title="tracemalloc.Statistic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Statistic</span></code></a> class.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff.count"> <span class="sig-name descname"><span class="pre">count</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff.count" title="Permalink to this definition">¶</a></dt> <dd><p>Number of memory blocks in the new snapshot (<code class="docutils literal notranslate"><span class="pre">int</span></code>): <code class="docutils literal notranslate"><span class="pre">0</span></code> if the memory blocks have been released in the new snapshot.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff.count_diff"> <span class="sig-name descname"><span class="pre">count_diff</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff.count_diff" title="Permalink to this definition">¶</a></dt> <dd><p>Difference of number of memory blocks between the old and the new snapshots (<code class="docutils literal notranslate"><span class="pre">int</span></code>): <code class="docutils literal notranslate"><span class="pre">0</span></code> if the memory blocks have been allocated in the new snapshot.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff.size"> <span class="sig-name descname"><span class="pre">size</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff.size" title="Permalink to this definition">¶</a></dt> <dd><p>Total size of memory blocks in bytes in the new snapshot (<code class="docutils literal notranslate"><span class="pre">int</span></code>): <code class="docutils literal notranslate"><span class="pre">0</span></code> if the memory blocks have been released in the new snapshot.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff.size_diff"> <span class="sig-name descname"><span class="pre">size_diff</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff.size_diff" title="Permalink to this definition">¶</a></dt> <dd><p>Difference of total size of memory blocks in bytes between the old and the new snapshots (<code class="docutils literal notranslate"><span class="pre">int</span></code>): <code class="docutils literal notranslate"><span class="pre">0</span></code> if the memory blocks have been allocated in the new snapshot.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.StatisticDiff.traceback"> <span class="sig-name descname"><span class="pre">traceback</span></span><a class="headerlink" href="#tracemalloc.StatisticDiff.traceback" title="Permalink to this definition">¶</a></dt> <dd><p>Traceback where the memory blocks were allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> instance.</p> </dd></dl> </dd></dl> </section> <section id="trace"> <h3>Trace<a class="headerlink" href="#trace" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Trace"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Trace</span></span><a class="headerlink" href="#tracemalloc.Trace" title="Permalink to this definition">¶</a></dt> <dd><p>Trace of a memory block.</p> <p>The <a class="reference internal" href="#tracemalloc.Snapshot.traces" title="tracemalloc.Snapshot.traces"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Snapshot.traces</span></code></a> attribute is a sequence of <a class="reference internal" href="#tracemalloc.Trace" title="tracemalloc.Trace"><code class="xref py py-class docutils literal notranslate"><span class="pre">Trace</span></code></a> instances.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <a class="reference internal" href="#tracemalloc.Trace.domain" title="tracemalloc.Trace.domain"><code class="xref py py-attr docutils literal notranslate"><span class="pre">domain</span></code></a> attribute.</p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Trace.domain"> <span class="sig-name descname"><span class="pre">domain</span></span><a class="headerlink" href="#tracemalloc.Trace.domain" title="Permalink to this definition">¶</a></dt> <dd><p>Address space of a memory block (<code class="docutils literal notranslate"><span class="pre">int</span></code>). Read-only property.</p> <p>tracemalloc uses the domain <code class="docutils literal notranslate"><span class="pre">0</span></code> to trace memory allocations made by Python. C extensions can use other domains to trace other resources.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Trace.size"> <span class="sig-name descname"><span class="pre">size</span></span><a class="headerlink" href="#tracemalloc.Trace.size" title="Permalink to this definition">¶</a></dt> <dd><p>Size of the memory block in bytes (<code class="docutils literal notranslate"><span class="pre">int</span></code>).</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Trace.traceback"> <span class="sig-name descname"><span class="pre">traceback</span></span><a class="headerlink" href="#tracemalloc.Trace.traceback" title="Permalink to this definition">¶</a></dt> <dd><p>Traceback where the memory block was allocated, <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> instance.</p> </dd></dl> </dd></dl> </section> <section id="traceback"> <h3>Traceback<a class="headerlink" href="#traceback" title="Permalink to this headline">¶</a></h3> <dl class="py class"> <dt class="sig sig-object py" id="tracemalloc.Traceback"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tracemalloc.</span></span><span class="sig-name descname"><span class="pre">Traceback</span></span><a class="headerlink" href="#tracemalloc.Traceback" title="Permalink to this definition">¶</a></dt> <dd><p>Sequence of <a class="reference internal" href="#tracemalloc.Frame" title="tracemalloc.Frame"><code class="xref py py-class docutils literal notranslate"><span class="pre">Frame</span></code></a> instances sorted from the oldest frame to the most recent frame.</p> <p>A traceback contains at least <code class="docutils literal notranslate"><span class="pre">1</span></code> frame. If the <code class="docutils literal notranslate"><span class="pre">tracemalloc</span></code> module failed to get a frame, the filename <code class="docutils literal notranslate"><span class="pre">"<unknown>"</span></code> at line number <code class="docutils literal notranslate"><span class="pre">0</span></code> is used.</p> <p>When a snapshot is taken, tracebacks of traces are limited to <a class="reference internal" href="#tracemalloc.get_traceback_limit" title="tracemalloc.get_traceback_limit"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_traceback_limit()</span></code></a> frames. See the <a class="reference internal" href="#tracemalloc.take_snapshot" title="tracemalloc.take_snapshot"><code class="xref py py-func docutils literal notranslate"><span class="pre">take_snapshot()</span></code></a> function. The original number of frames of the traceback is stored in the <a class="reference internal" href="#tracemalloc.Traceback.total_nframe" title="tracemalloc.Traceback.total_nframe"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Traceback.total_nframe</span></code></a> attribute. That allows to know if a traceback has been truncated by the traceback limit.</p> <p>The <a class="reference internal" href="#tracemalloc.Trace.traceback" title="tracemalloc.Trace.traceback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Trace.traceback</span></code></a> attribute is an instance of <a class="reference internal" href="#tracemalloc.Traceback" title="tracemalloc.Traceback"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traceback</span></code></a> instance.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Frames are now sorted from the oldest to the most recent, instead of most recent to oldest.</p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="tracemalloc.Traceback.total_nframe"> <span class="sig-name descname"><span class="pre">total_nframe</span></span><a class="headerlink" href="#tracemalloc.Traceback.total_nframe" title="Permalink to this definition">¶</a></dt> <dd><p>Total number of frames that composed the traceback before truncation. This attribute can be set to <code class="docutils literal notranslate"><span class="pre">None</span></code> if the information is not available.</p> </dd></dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>The <a class="reference internal" href="#tracemalloc.Traceback.total_nframe" title="tracemalloc.Traceback.total_nframe"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Traceback.total_nframe</span></code></a> attribute was added.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="tracemalloc.Traceback.format"> <span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">most_recent_first</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tracemalloc.Traceback.format" title="Permalink to this definition">¶</a></dt> <dd><p>Format the traceback as a list of lines. Use the <a class="reference internal" href="linecache.html#module-linecache" title="linecache: Provides random access to individual lines from text files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">linecache</span></code></a> module to retrieve lines from the source code. If <em>limit</em> is set, format the <em>limit</em> most recent frames if <em>limit</em> is positive. Otherwise, format the <code class="docutils literal notranslate"><span class="pre">abs(limit)</span></code> oldest frames. If <em>most_recent_first</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the order of the formatted frames is reversed, returning the most recent frame first instead of last.</p> <p>Similar to the <a class="reference internal" href="traceback.html#traceback.format_tb" title="traceback.format_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">traceback.format_tb()</span></code></a> function, except that <a class="reference internal" href="#tracemalloc.Traceback.format" title="tracemalloc.Traceback.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> does not include newlines.</p> <p>Example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"Traceback (most recent call first):"</span><span class="p">)</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">traceback</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> </pre></div> </div> <p>Output:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">first</span><span class="p">):</span> <span class="n">File</span> <span class="s2">"test.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">9</span> <span class="n">obj</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span> <span class="n">File</span> <span class="s2">"test.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">12</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">tracemalloc</span><span class="o">.</span><span class="n">get_object_traceback</span><span class="p">(</span><span class="n">f</span><span class="p">())</span> </pre></div> </div> </dd></dl> </dd></dl> </section> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a><ul> <li><a class="reference internal" href="#examples">Examples</a><ul> <li><a class="reference internal" href="#display-the-top-10">Display the top 10</a></li> <li><a class="reference internal" href="#compute-differences">Compute differences</a></li> <li><a class="reference internal" href="#get-the-traceback-of-a-memory-block">Get the traceback of a memory block</a></li> <li><a class="reference internal" href="#pretty-top">Pretty top</a><ul> <li><a class="reference internal" href="#record-the-current-and-peak-size-of-all-traced-memory-blocks">Record the current and peak size of all traced memory blocks</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#api">API</a><ul> <li><a class="reference internal" href="#functions">Functions</a></li> <li><a class="reference internal" href="#domainfilter">DomainFilter</a></li> <li><a class="reference internal" href="#filter">Filter</a></li> <li><a class="reference internal" href="#frame">Frame</a></li> <li><a class="reference internal" href="#snapshot">Snapshot</a></li> <li><a class="reference internal" href="#statistic">Statistic</a></li> <li><a class="reference internal" href="#statisticdiff">StatisticDiff</a></li> <li><a class="reference internal" href="#trace">Trace</a></li> <li><a class="reference internal" href="#traceback">Traceback</a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="trace.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a></p> <h4>Next topic</h4> <p class="topless"><a href="distribution.html" title="next chapter">Software Packaging and Distribution</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/tracemalloc.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="distribution.html" title="Software Packaging and Distribution" >next</a> |</li> <li class="right" > <a href="trace.html" title="trace — Trace or track Python statement execution" >previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="debug.html" >Debugging and Profiling</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2026, Python Software Foundation. <br /> This page is licensed under the Python Software Foundation License Version 2. <br /> Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. <br /> See <a href="/license.html">History and License</a> for more information.<br /> <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> <br /> Last updated on January 26, 2026. <a href="/bugs.html">Found a bug</a>? <br /> Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2. </div> </body> </html>
Close