Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Performance and Profiling

memory_profiler and tracemalloc

Performance and Profiling

Introduction

Execution time is not everything. Many Python programs fail not because they are slow but because they eat all the RAM. Memory profiling is the second pillar of performance — it answers: "why did my script gobble 8 GB?".

Two main tools: memory_profiler (third-party package, @profile decorator measuring memory line-by-line) and tracemalloc (stdlib since 3.4, tracks allocations through the CPython module and exposes snapshots + diff statistics).

This lesson covers: how @profile from memory_profiler works, how to read "Increment" deltas, mprof run / mprof plot, tracemalloc snapshots and compare_to, hunting leaks, the role of gc, sys.getsizeof (and its pitfalls), and memory tricks: generators, __slots__, array.array/numpy.