-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-130695: add count create/destroy refs to tracemalloc #130696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3db607f
cfdaaee
0bb3261
4f7d8a8
7432709
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,7 +299,8 @@ Functions | |
|
||
.. function:: clear_traces() | ||
|
||
Clear traces of memory blocks allocated by Python. | ||
Clear traces of memory blocks allocated and counts of created and destroyed | ||
memory blocks by Python. | ||
|
||
See also :func:`stop`. | ||
|
||
|
@@ -330,6 +331,14 @@ Functions | |
:mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``. | ||
|
||
|
||
.. function:: get_traced_refs() | ||
|
||
Get the current count of created and destroyed traced references. | ||
:mod:`tracemalloc` module as a tuple: ``(created: int, destroyed: int)``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this sentence: "tracemalloc module as a tuple"? |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should explain what are "references" here. They are not the classical reference count. Maybe explain the difference with the reference count? |
||
.. versionadded:: next | ||
|
||
tom-pytel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. function:: reset_peak() | ||
|
||
Set the peak size of memory blocks traced by the :mod:`tracemalloc` module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import textwrap | ||
import tracemalloc | ||
import unittest | ||
import warnings | ||
from unittest.mock import patch | ||
from test.support.script_helper import (assert_python_ok, assert_python_failure, | ||
interpreter_requires_environment) | ||
|
@@ -1141,6 +1142,37 @@ def __del__(self, untrack=_testcapi.tracemalloc_untrack): | |
""") | ||
assert_python_ok("-c", code) | ||
|
||
def test_trace_refs(self): | ||
def f(): | ||
l = [] | ||
del l | ||
|
||
def g(): | ||
l = [], [] | ||
del l | ||
|
||
tracemalloc.start() | ||
|
||
try: | ||
tracemalloc.clear_traces() | ||
f() | ||
refs = tracemalloc.get_traced_refs() | ||
if refs == (1, 0): | ||
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect a test failure, not a warning. In which case we would fall into this case? And why ignoring this failure? |
||
else: | ||
self.assertEqual(refs, (1, 1)) | ||
|
||
tracemalloc.clear_traces() | ||
g() | ||
refs = tracemalloc.get_traced_refs() | ||
if refs == (3, 2): | ||
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build") | ||
else: | ||
self.assertEqual(refs, (3, 3)) | ||
|
||
finally: | ||
tracemalloc.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tracking of number of deallocations to :mod:`tracemalloc`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.