Memory

Chester Wyke August 16, 2023 Updated: April 15, 2025 #python

Source: https://docs.python.org/3/library/tracemalloc.html

See excerpt from introductory paragraph

To trace most memory blocks allocated by Python, the module should be started as early as possible by setting the PYTHONTRACEMALLOC environment variable to 1, or by using -X tracemalloc command line option. The tracemalloc.start() function can be called at runtime to start tracing Python memory allocations.

See docs for full example

import tracemalloc

tracemalloc.start()

size, peak = tracemalloc.get_traced_memory()