mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
[3.9] bpo-40630: Add tracemalloc.reset_peak (GH-20102) (GH-20545)
* bpo-40630: Add tracemalloc.reset_peak (GH-20102, cherrypick 8b62644
)
The reset_peak function sets the peak memory size to the current size,
representing a resetting of that metric. This allows for recording the
peak of specific sections of code, ignoring other code that may have
had a higher peak (since the most recent `tracemalloc.start()` or
tracemalloc.clear_traces()` call).
* Adjust docs to point to 3.9
This commit is contained in:
parent
410b730c20
commit
3c7609a23c
7 changed files with 138 additions and 1 deletions
|
@ -246,6 +246,30 @@ class TestTracemallocEnabled(unittest.TestCase):
|
|||
traceback2 = tracemalloc.get_object_traceback(obj)
|
||||
self.assertIsNone(traceback2)
|
||||
|
||||
def test_reset_peak(self):
|
||||
# Python allocates some internals objects, so the test must tolerate
|
||||
# a small difference between the expected size and the real usage
|
||||
tracemalloc.clear_traces()
|
||||
|
||||
# Example: allocate a large piece of memory, temporarily
|
||||
large_sum = sum(list(range(100000)))
|
||||
size1, peak1 = tracemalloc.get_traced_memory()
|
||||
|
||||
# reset_peak() resets peak to traced memory: peak2 < peak1
|
||||
tracemalloc.reset_peak()
|
||||
size2, peak2 = tracemalloc.get_traced_memory()
|
||||
self.assertGreaterEqual(peak2, size2)
|
||||
self.assertLess(peak2, peak1)
|
||||
|
||||
# check that peak continue to be updated if new memory is allocated:
|
||||
# peak3 > peak2
|
||||
obj_size = 1024 * 1024
|
||||
obj, obj_traceback = allocate_bytes(obj_size)
|
||||
size3, peak3 = tracemalloc.get_traced_memory()
|
||||
self.assertGreaterEqual(peak3, size3)
|
||||
self.assertGreater(peak3, peak2)
|
||||
self.assertGreaterEqual(peak3 - peak2, obj_size)
|
||||
|
||||
def test_is_tracing(self):
|
||||
tracemalloc.stop()
|
||||
self.assertFalse(tracemalloc.is_tracing())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue