Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 637d2e9

Browse files
committed
Issue python#20888: improve "Pretty Top" example of tracemalloc, use linecache
1 parent 0c18282 commit 637d2e9

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

Doc/library/tracemalloc.rst

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Pretty top
184184
Code to display the 10 lines allocating the most memory with a pretty output,
185185
ignoring ``<frozen importlib._bootstrap>`` and ``<unknown>`` files::
186186

187+
import linecache
187188
import os
188189
import tracemalloc
189190

@@ -201,6 +202,9 @@ ignoring ``<frozen importlib._bootstrap>`` and ``<unknown>`` files::
201202
filename = os.sep.join(frame.filename.split(os.sep)[-2:])
202203
print("#%s: %s:%s: %.1f KiB"
203204
% (index, filename, frame.lineno, stat.size / 1024))
205+
line = linecache.getline(frame.filename, frame.lineno).strip()
206+
if line:
207+
print(' %s' % line)
204208

205209
other = top_stats[limit:]
206210
if other:
@@ -218,19 +222,28 @@ ignoring ``<frozen importlib._bootstrap>`` and ``<unknown>`` files::
218222

219223
Example of output of the Python test suite::
220224

221-
2013-11-08 14:16:58.149320: Top 10 lines
222-
#1: collections/__init__.py:368: 291.9 KiB
223-
#2: Lib/doctest.py:1291: 200.2 KiB
224-
#3: unittest/case.py:571: 160.3 KiB
225-
#4: Lib/abc.py:133: 99.8 KiB
226-
#5: urllib/parse.py:476: 71.8 KiB
227-
#6: <string>:5: 62.7 KiB
228-
#7: Lib/base64.py:140: 59.8 KiB
229-
#8: Lib/_weakrefset.py:37: 51.8 KiB
230-
#9: collections/__init__.py:362: 50.6 KiB
231-
#10: test/test_site.py:56: 48.0 KiB
232-
7496 other: 4161.9 KiB
233-
Total allocated size: 5258.8 KiB
225+
Top 10 lines
226+
#1: Lib/base64.py:414: 419.8 KiB
227+
_b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
228+
#2: Lib/base64.py:306: 419.8 KiB
229+
_a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
230+
#3: collections/__init__.py:368: 293.6 KiB
231+
exec(class_definition, namespace)
232+
#4: Lib/abc.py:133: 115.2 KiB
233+
cls = super().__new__(mcls, name, bases, namespace)
234+
#5: unittest/case.py:574: 103.1 KiB
235+
testMethod()
236+
#6: Lib/linecache.py:127: 95.4 KiB
237+
lines = fp.readlines()
238+
#7: urllib/parse.py:476: 71.8 KiB
239+
for a in _hexdig for b in _hexdig}
240+
#8: <string>:5: 62.0 KiB
241+
#9: Lib/_weakrefset.py:37: 60.0 KiB
242+
self.data = set()
243+
#10: Lib/base64.py:142: 59.8 KiB
244+
_b32tab2 = [a + b for a in _b32tab for b in _b32tab]
245+
6220 other: 3602.8 KiB
246+
Total allocated size: 5303.1 KiB
234247

235248
See :meth:`Snapshot.statistics` for more options.
236249

0 commit comments

Comments
 (0)