October 13, 2024

Python memory-profiler Module

The memory-profiler module is a Python library used for profiling memory usage of a program. It provides insights into how much memory is consumed by different parts of your code, helping to identify memory leaks and optimize memory usage.

1. Installation

You can install the memory-profiler module using pip:

pip install memory-profiler
    

2. Basic Usage

Here is how you can use memory-profiler to profile a Python function:

from memory_profiler import profile

@profile
def my_function():
    a = [1] * (10**6)  # Allocate a list of 1 million integers
    b = [2] * (2 * 10**7)  # Allocate a larger list
    del b  # Free the larger list

if __name__ == "__main__":
    my_function()
    

3. Command-Line Interface

You can also use memory-profiler from the command line to profile a script. For example:

python -m memory_profiler my_script.py
    

4. Profiling Specific Lines

To profile specific lines of a function, use the @profile decorator, as shown in the basic usage example. You can also use the mprof command for detailed memory profiling over time:

mprof run my_script.py
mprof plot
    

5. Conclusion

The memory-profiler module is a valuable tool for analyzing and optimizing memory usage in Python programs. By profiling your code, you can identify memory bottlenecks and improve the efficiency of your applications.