From eb6c9ac81d6bacbd5ef5b79a0b79c75ebc1b6de8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 15 May 2020 13:22:24 +1000 Subject: [PATCH 1/9] bpo-40630: add tracemalloc.reset_peak 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). --- Doc/library/tracemalloc.rst | 20 +++++++++++++ Lib/test/test_tracemalloc.py | 29 +++++++++++++++++++ Misc/ACKS | 1 + .../2020-05-15-13-40-15.bpo-40630.YXEX_M.rst | 2 ++ Modules/_tracemalloc.c | 23 +++++++++++++++ Modules/clinic/_tracemalloc.c.h | 20 ++++++++++++- 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-15-13-40-15.bpo-40630.YXEX_M.rst diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 3eee9457fb29a8..33224e4e4740df 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -289,6 +289,26 @@ Functions :mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``. +.. function:: reset_peak() + + Set the peak size of memory blocks traced by the :mod:`tracemalloc` module + to the current size. + + The peak memory usage of a specific section of code can be determined as + follows:: + + tracemalloc.reset_peak() + + # some code + + current, peak = tracemalloc.get_traced_memory() + + ``peak`` will then be the peak size of traced memory blocks after the + :func:`reset_peak` call, even if there was a higher peak before that. + + See also :func:`get_traced_memory`. + + .. function:: get_tracemalloc_memory() Get the memory usage in bytes of the :mod:`tracemalloc` module used to store diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index 635a9d39816058..eae72de8892d29 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -246,6 +246,35 @@ def test_clear_traces(self): traceback2 = tracemalloc.get_object_traceback(obj) self.assertIsNone(traceback2) + def test_reset_peak(self): + max_error = 2048 + def valid(size): + return range(size, size + max_error) + + obj_size = 1024 * 1024 + + tracemalloc.clear_traces() + + obj, obj_traceback = allocate_bytes(obj_size) + # add to the peak + allocate_bytes(obj_size) + + size, peak_size = tracemalloc.get_traced_memory() + self.assertIn(peak_size, valid(2 * obj_size)) + + tracemalloc.reset_peak() + + size2, peak_size2 = tracemalloc.get_traced_memory() + self.assertIn(size2, valid(size)) + # the peak should include 'obj', but not the other allocation + self.assertIn(peak_size2, valid(size2)) + + # the peak should continue to be updated + allocate_bytes(obj_size) + size3, peak_size3 = tracemalloc.get_traced_memory() + self.assertIn(size3, valid(obj_size)) + self.assertIn(peak_size3, valid(2 * obj_size)) + def test_is_tracing(self): tracemalloc.stop() self.assertFalse(tracemalloc.is_tracing()) diff --git a/Misc/ACKS b/Misc/ACKS index f744de6b1f66d2..dd29717c438c6b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1860,6 +1860,7 @@ Alex Willmer David Wilson Geoff Wilson Greg V. Wilson +Huon Wilson J Derek Wilson Paul Winkler Jody Winston diff --git a/Misc/NEWS.d/next/Library/2020-05-15-13-40-15.bpo-40630.YXEX_M.rst b/Misc/NEWS.d/next/Library/2020-05-15-13-40-15.bpo-40630.YXEX_M.rst new file mode 100644 index 00000000000000..bb2e7452d3cfbe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-15-13-40-15.bpo-40630.YXEX_M.rst @@ -0,0 +1,2 @@ +Added :func:`tracemalloc.reset_peak` to set the peak size of traced memory +blocks to the current size, to measure the peak of specific pieces of code. diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 4522d1afde9089..47b33859430dec 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -1643,6 +1643,28 @@ _tracemalloc_get_traced_memory_impl(PyObject *module) return Py_BuildValue("nn", size, peak_size); } +/*[clinic input] +_tracemalloc.reset_peak + +Set the peak size of memory blocks traced by tracemalloc to the current size. + +[clinic start generated code]*/ + +static PyObject * +_tracemalloc_reset_peak_impl(PyObject *module) +/*[clinic end generated code: output=140c2870f691dbb2 input=5a7e962ebebf9836]*/ +{ + if (!_Py_tracemalloc_config.tracing) { + Py_RETURN_NONE; + } + + TABLES_LOCK(); + tracemalloc_peak_traced_memory = tracemalloc_traced_memory; + TABLES_UNLOCK(); + + Py_RETURN_NONE; +} + static PyMethodDef module_methods[] = { _TRACEMALLOC_IS_TRACING_METHODDEF @@ -1654,6 +1676,7 @@ static PyMethodDef module_methods[] = { _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF + _TRACEMALLOC_RESET_PEAK_METHODDEF /* sentinel */ {NULL, NULL} }; diff --git a/Modules/clinic/_tracemalloc.c.h b/Modules/clinic/_tracemalloc.c.h index 68fafdc3833d2e..d79d962f9bc0fa 100644 --- a/Modules/clinic/_tracemalloc.c.h +++ b/Modules/clinic/_tracemalloc.c.h @@ -197,4 +197,22 @@ _tracemalloc_get_traced_memory(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _tracemalloc_get_traced_memory_impl(module); } -/*[clinic end generated code: output=1bc96dc569706afa input=a9049054013a1b77]*/ + +PyDoc_STRVAR(_tracemalloc_reset_peak__doc__, +"reset_peak($module, /)\n" +"--\n" +"\n" +"Set the peak size of memory blocks traced by tracemalloc to the current size."); + +#define _TRACEMALLOC_RESET_PEAK_METHODDEF \ + {"reset_peak", (PyCFunction)_tracemalloc_reset_peak, METH_NOARGS, _tracemalloc_reset_peak__doc__}, + +static PyObject * +_tracemalloc_reset_peak_impl(PyObject *module); + +static PyObject * +_tracemalloc_reset_peak(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _tracemalloc_reset_peak_impl(module); +} +/*[clinic end generated code: output=ed93fb2537bfde72 input=a9049054013a1b77]*/ From d1b1e3306acd162e4f1146e453f5afc182d99b5e Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 16 May 2020 08:45:08 +1000 Subject: [PATCH 2/9] Move example documentation --- Doc/library/tracemalloc.rst | 51 ++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 33224e4e4740df..1a6a1a551fb305 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -249,6 +249,45 @@ Example of output of the Python test suite:: See :meth:`Snapshot.statistics` for more options. +Get the peak memory usage of a specific piece of code +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Code to determine the peak memory usage of a specific section of code:: + + import tracemalloc + + tracemalloc.start() + + # Example: allocate a large piece of memory, temporarily + large_sum = sum(list(range(100000))) + + first_size, first_peak = tracemalloc.get_traced_memory() + + tracemalloc.reset_peak() + + # Example: allocate a smaller piece of memory + small_sum = sum(list(range(1000))) + + second_size, second_peak = tracemalloc.get_traced_memory() + + print(f"{first_size=}, {first_peak=}") + print(f"{second_size=}, {second_peak=}") + +Output:: + + first_size=664, first_peak=3592984 + second_size=804, second_peak=29704 + +``second_peak`` is the peak size of traced memory blocks during the computation +of ``small_sum`` (after the :func:`reset_peak` call). Notably, the computation +of ``large_sum`` results in a higher peak, but we have still been able to +observe a smaller peak. 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 :class:`list`, and writing ``sum(range(...))``). + +:func:`reset_peak` only modifies the recorded peak size, and does not modify or +clear any traces. Snapshots from before a call to :func:`reset_peak` can be +meaningfully compared to snapshots after the call. API --- @@ -294,18 +333,6 @@ Functions Set the peak size of memory blocks traced by the :mod:`tracemalloc` module to the current size. - The peak memory usage of a specific section of code can be determined as - follows:: - - tracemalloc.reset_peak() - - # some code - - current, peak = tracemalloc.get_traced_memory() - - ``peak`` will then be the peak size of traced memory blocks after the - :func:`reset_peak` call, even if there was a higher peak before that. - See also :func:`get_traced_memory`. From 6896e8cc15ea07d6a61b9d37d37ec29713053c39 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 17 May 2020 10:55:08 +1000 Subject: [PATCH 3/9] Generalise documentation to focus on current and peak --- Doc/library/tracemalloc.rst | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 1a6a1a551fb305..90f490ce12f74b 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -249,23 +249,27 @@ Example of output of the Python test suite:: See :meth:`Snapshot.statistics` for more options. -Get the peak memory usage of a specific piece of code -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Record the current and peak size of all traced memory blocks +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Code to determine the peak memory usage of a specific section of code:: +The following code computes two sums like ``0 + 1 + 2 + ...`` inefficiently, by +creating a list of those numbers. This list consumes a lot of memory +temporarily. We can use :func:`get_traced_memory` and :func:`reset_peak` to +observe the small memory usage after the sum is computed as well as the peak +memory usage during the computations:: import tracemalloc tracemalloc.start() - # Example: allocate a large piece of memory, temporarily + # Example code: compute a sum with a large temporary list large_sum = sum(list(range(100000))) first_size, first_peak = tracemalloc.get_traced_memory() tracemalloc.reset_peak() - # Example: allocate a smaller piece of memory + # Example code: compute a sum with a small temporary list small_sum = sum(list(range(1000))) second_size, second_peak = tracemalloc.get_traced_memory() @@ -278,16 +282,14 @@ Output:: first_size=664, first_peak=3592984 second_size=804, second_peak=29704 -``second_peak`` is the peak size of traced memory blocks during the computation -of ``small_sum`` (after the :func:`reset_peak` call). Notably, the computation -of ``large_sum`` results in a higher peak, but we have still been able to -observe a smaller peak. 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 :class:`list`, and writing ``sum(range(...))``). - -:func:`reset_peak` only modifies the recorded peak size, and does not modify or -clear any traces. Snapshots from before a call to :func:`reset_peak` can be -meaningfully compared to snapshots after the call. +Using :func:`reset_peak` ensured we could accurately record the peak during the +computation of ``small_sum``, even though it is much smaller than the overall +peak size of memory blocks since the :func:`start` call. Without the call to +:func:`reset_peak`, ``second_peak`` would still be the peak from the +computation ``large_sum`` (that is, equal to ``first_peak``). 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 :class:`list`, and writing +``sum(range(...))``). API --- @@ -333,6 +335,11 @@ Functions Set the peak size of memory blocks traced by the :mod:`tracemalloc` module to the current size. + This function only modifies the recorded peak size, and does not modify or + clear any traces, unlike :func:`clear_traces`. Snapshots taken with + :func:`take_snapshot` before a call to :func:`reset_peak` can be + meaningfully compared to snapshots taken after the call. + See also :func:`get_traced_memory`. From b5dc0588888898d344d07429f7e2bbdf787528ee Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 17 May 2020 10:56:57 +1000 Subject: [PATCH 4/9] Use suggested test --- Lib/test/test_tracemalloc.py | 44 +++++++++++++----------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index eae72de8892d29..96e7eaa5b1f008 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -201,41 +201,29 @@ def allocate_bytes4(size): domain2, size2, traceback2, length2 = trace2 self.assertIs(traceback2, traceback1) - def test_get_traced_memory(self): + 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 - max_error = 2048 - - # allocate one object - obj_size = 1024 * 1024 tracemalloc.clear_traces() - obj, obj_traceback = allocate_bytes(obj_size) - size, peak_size = tracemalloc.get_traced_memory() - self.assertGreaterEqual(size, obj_size) - self.assertGreaterEqual(peak_size, size) - self.assertLessEqual(size - obj_size, max_error) - self.assertLessEqual(peak_size - size, max_error) + # Example: allocate a large piece of memory, temporarily + large_sum = sum(list(range(100000))) + size1, peak1 = tracemalloc.get_traced_memory() - # destroy the object - obj = None - size2, peak_size2 = tracemalloc.get_traced_memory() - self.assertLess(size2, size) - self.assertGreaterEqual(size - size2, obj_size - max_error) - self.assertGreaterEqual(peak_size2, peak_size) - - # clear_traces() must reset traced memory counters - tracemalloc.clear_traces() - self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) + # 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) - # allocate another object + # 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) - size, peak_size = tracemalloc.get_traced_memory() - self.assertGreaterEqual(size, obj_size) - - # stop() also resets traced memory counters - tracemalloc.stop() - self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) + size3, peak3 = tracemalloc.get_traced_memory() + self.assertGreaterEqual(peak3, size3) + self.assertGreater(peak3, peak2) + self.assertGreaterEqual(peak3 - peak2, obj_size) def test_clear_traces(self): obj, obj_traceback = allocate_bytes(123) From 2da86b34934f6170a3558bb720db67b0c8eba622 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 17 May 2020 10:58:19 +1000 Subject: [PATCH 5/9] Oops, update the correct test. --- Lib/test/test_tracemalloc.py | 83 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index 96e7eaa5b1f008..c5ae4e6d653bf7 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -201,29 +201,41 @@ def allocate_bytes4(size): domain2, size2, traceback2, length2 = trace2 self.assertIs(traceback2, traceback1) - def test_reset_peak(self): + def test_get_traced_memory(self): # Python allocates some internals objects, so the test must tolerate # a small difference between the expected size and the real usage + max_error = 2048 + + # allocate one object + obj_size = 1024 * 1024 tracemalloc.clear_traces() + obj, obj_traceback = allocate_bytes(obj_size) + size, peak_size = tracemalloc.get_traced_memory() + self.assertGreaterEqual(size, obj_size) + self.assertGreaterEqual(peak_size, size) - # Example: allocate a large piece of memory, temporarily - large_sum = sum(list(range(100000))) - size1, peak1 = tracemalloc.get_traced_memory() + self.assertLessEqual(size - obj_size, max_error) + self.assertLessEqual(peak_size - size, max_error) - # 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) + # destroy the object + obj = None + size2, peak_size2 = tracemalloc.get_traced_memory() + self.assertLess(size2, size) + self.assertGreaterEqual(size - size2, obj_size - max_error) + self.assertGreaterEqual(peak_size2, peak_size) - # check that peak continue to be updated if new memory is allocated: - # peak3 > peak2 - obj_size = 1024 * 1024 + # clear_traces() must reset traced memory counters + tracemalloc.clear_traces() + self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) + + # allocate another object 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) + size, peak_size = tracemalloc.get_traced_memory() + self.assertGreaterEqual(size, obj_size) + + # stop() also resets traced memory counters + tracemalloc.stop() + self.assertEqual(tracemalloc.get_traced_memory(), (0, 0)) def test_clear_traces(self): obj, obj_traceback = allocate_bytes(123) @@ -235,33 +247,28 @@ def test_clear_traces(self): self.assertIsNone(traceback2) def test_reset_peak(self): - max_error = 2048 - def valid(size): - return range(size, size + max_error) - - obj_size = 1024 * 1024 - + # Python allocates some internals objects, so the test must tolerate + # a small difference between the expected size and the real usage tracemalloc.clear_traces() - obj, obj_traceback = allocate_bytes(obj_size) - # add to the peak - allocate_bytes(obj_size) - - size, peak_size = tracemalloc.get_traced_memory() - self.assertIn(peak_size, valid(2 * obj_size)) + # 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) - size2, peak_size2 = tracemalloc.get_traced_memory() - self.assertIn(size2, valid(size)) - # the peak should include 'obj', but not the other allocation - self.assertIn(peak_size2, valid(size2)) - - # the peak should continue to be updated - allocate_bytes(obj_size) - size3, peak_size3 = tracemalloc.get_traced_memory() - self.assertIn(size3, valid(obj_size)) - self.assertIn(peak_size3, valid(2 * obj_size)) + # 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() From 218993e9b6849dc750f1ddd6e1a47a5524c8628a Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 19 May 2020 09:01:19 +1000 Subject: [PATCH 6/9] Add note about not tracing --- Doc/library/tracemalloc.rst | 3 +++ Modules/_tracemalloc.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index 90f490ce12f74b..df2728426344da 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -335,6 +335,9 @@ Functions Set the peak size of memory blocks traced by the :mod:`tracemalloc` module to the current size. + Do nothing if the :mod:`tracemalloc` module is not tracing memory + allocations. + This function only modifies the recorded peak size, and does not modify or clear any traces, unlike :func:`clear_traces`. Snapshots taken with :func:`take_snapshot` before a call to :func:`reset_peak` can be diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 47b33859430dec..3a463754a07f21 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -1648,6 +1648,8 @@ _tracemalloc.reset_peak Set the peak size of memory blocks traced by tracemalloc to the current size. +Do nothing if the tracemalloc module is not tracing memory allocations. + [clinic start generated code]*/ static PyObject * From 9060f62635b34f593c15cba7a8bdff4bb8d3c2ce Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 19 May 2020 11:04:05 +1000 Subject: [PATCH 7/9] Rerun clinic --- Modules/_tracemalloc.c | 2 +- Modules/clinic/_tracemalloc.c.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 3a463754a07f21..567571657453e7 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -1654,7 +1654,7 @@ Do nothing if the tracemalloc module is not tracing memory allocations. static PyObject * _tracemalloc_reset_peak_impl(PyObject *module) -/*[clinic end generated code: output=140c2870f691dbb2 input=5a7e962ebebf9836]*/ +/*[clinic end generated code: output=140c2870f691dbb2 input=18afd0635066e9ce]*/ { if (!_Py_tracemalloc_config.tracing) { Py_RETURN_NONE; diff --git a/Modules/clinic/_tracemalloc.c.h b/Modules/clinic/_tracemalloc.c.h index d79d962f9bc0fa..049cacd8326638 100644 --- a/Modules/clinic/_tracemalloc.c.h +++ b/Modules/clinic/_tracemalloc.c.h @@ -202,7 +202,9 @@ PyDoc_STRVAR(_tracemalloc_reset_peak__doc__, "reset_peak($module, /)\n" "--\n" "\n" -"Set the peak size of memory blocks traced by tracemalloc to the current size."); +"Set the peak size of memory blocks traced by tracemalloc to the current size.\n" +"\n" +"Do nothing if the tracemalloc module is not tracing memory allocations."); #define _TRACEMALLOC_RESET_PEAK_METHODDEF \ {"reset_peak", (PyCFunction)_tracemalloc_reset_peak, METH_NOARGS, _tracemalloc_reset_peak__doc__}, @@ -215,4 +217,4 @@ _tracemalloc_reset_peak(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _tracemalloc_reset_peak_impl(module); } -/*[clinic end generated code: output=ed93fb2537bfde72 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a130117b1af821da input=a9049054013a1b77]*/ From 9deeb01587c2a0d8d36d3a085d72b7a1a6bbf7ac Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 20 May 2020 15:18:17 +1000 Subject: [PATCH 8/9] Add to whatsnew --- Doc/whatsnew/3.10.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 547778599ef617..e650f9405a811d 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -86,6 +86,12 @@ New Modules Improved Modules ================ +tracemalloc +----------- + +Added :func:`tracemalloc.reset_peak` to set the peak size of traced memory +blocks to the current size, to measure the peak of specific pieces of code. +(Contributed by Huon Wilson in :issue:`40630`.) Optimizations ============= From e44ca731dc97433ba8c66caed77faafd8d11ef8c Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 22 May 2020 16:23:33 +1000 Subject: [PATCH 9/9] Add versionadded directive --- Doc/library/tracemalloc.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/tracemalloc.rst b/Doc/library/tracemalloc.rst index df2728426344da..fba1caab455d73 100644 --- a/Doc/library/tracemalloc.rst +++ b/Doc/library/tracemalloc.rst @@ -345,6 +345,8 @@ Functions See also :func:`get_traced_memory`. + .. versionadded:: 3.10 + .. function:: get_tracemalloc_memory()