Total memory used by Python process?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Python is an incredibly popular language owing to its simplicity and versatility. However, one of the core considerations when developing Python applications is understanding how much memory your Python process consumes. Memory usage can directly impact the performance, scalability, and cost of running applications.
Understanding Memory in a Python Process
A Python process, like any other process, uses memory for code execution, data storage, and stack memory. However, due to Python's nature and runtime environment, it incorporates several additional layers of memory usage, including:
- PVM (Python Virtual Machine): This is responsible for executing the Python byte code. It takes some memory to operate.
- Interpreter Overhead: Python's dynamic typing and garbage collection add extra overhead.
- Loaded Modules and Libraries: Every imported module or library uses additional memory.
- Data Structures: Python's built-in data structures like lists, dictionaries, and sets can be memory-intensive due to their dynamic-sizing features.
Factors Affecting Total Memory Usage
- Python Objects and Their Internals:Python objects have an inherent memory overhead due to their attribute dictionaries and dynamic typing. The size adds up with the number of objects and their relationships (i.e., references).
- Garbage Collector:Python utilizes a cyclic garbage collector which, while managing memory allocation and deallocation, temporarily consumes additional memory.
- Multithreading and Multiprocessing:Threading in Python is not true multi-core (due to the Global Interpreter Lock), but still uses resources. Using the
multiprocessingmodule creates separate memory spaces for each process, increasing the total memory usage. - External Libraries:Libraries such as NumPy or Pandas provide powerful capabilities at the cost of additional memory usage, especially with large datasets.
Measuring Memory Usage
To measure the total memory usage of a Python process, you can use various libraries and utilities, including:
psutil: This cross-platform library provides an easy way to obtain system and process information including memory usage.tracemalloc: In Python 3.4+, this module helps track memory allocations. It gives insights into where memory is used, aiding in pinpointing memory bloat.
Related reading
- Towers of Hanoi with K pegs
- Trained Machine Learning model is too big
- Training a simple model in Tensorflow GPU slower than CPU
- Training broke with ResourceExausted error
- tqdm in Jupyter Notebook prints new progress bars repeatedly
- Training a Neural Network in Python and deploying in C
- training by batches leads to more over-fitting
- Training of keras model get's slower after each repetition

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.