DEV Community

moogoo
moogoo

Posted on

Check Python Memory Usage

Build-in libraries

import tracemalloc

print(tracemalloc.start())
# ...
# my ugly code
# ...
print(tracemalloc.get_traced_memory())

# shows (current_usage, peak)
Enter fullscreen mode Exit fullscreen mode

Tools

# need pip install memory_profiler
from memory_profiler import profile

@profile
def my_ugly_func():
  # ...
  # ...

# show memory usage line by line
Enter fullscreen mode Exit fullscreen mode

Other

Tkinter initial use 41.9 MB

Image description

Top comments (0)