Python Memory Profiler is a Python package designed for efficient and customizable memory usage monitoring of Python programs. It allows developers to track memory metrics such as data, RSS (Resident Set Size), swap usage, and USS (Unique Set Size) for specific functions in their applications.
- Lightweight Memory Profiling: Monitors memory metrics for specific functions.
- Configurable Settings: Adjustable sampling frequency and profiling duration.
- Visualization: Automatically generates plots of memory usage trends.
- Data Persistence: Saves memory metrics for further analysis.
pip install python-memory-profilerIf you want to install the dev version, clone the repository and install the dependencies:
$ git clone https://github.com/GhasseneJebali/python-memory-profiler.git
$ cd python-memory-profiler
$ pip install -e . -r requirements.txtYou can use the @profile_memory_decorator to profile memory usage for specific functions.
from memory_profiler.decorators import memory_profiler_decorator
@memory_profiler_decorator
def my_function():
# Your code here
pass
if __name__ == "__main__":
my_function()This will:
- Monitor the memory usage of
my_function. - Save the memory metrics in the
datafolder. - Generate a memory usage plot.
For more control, you can directly use the Profiler class:
import os
from memory_profiler.src.profiler import Profiler
if __name__ == "__main__":
pid = os.getpid()
profiler = Profiler(pid, "my_function")
profiler.start()
# Your code here
# If monitor is not specified, all metrics will be logged
# Logged metrics are data, rss, uss and swap
profiler.save(monitor=None)
profiler.plot(monitor=None)If you installed the package from source, you can adjust default profiling parameters such as sampling frequency and output path by modifying the src/configs/config.yaml configuration file:
max_timer: 0 # Maximum time (in seconds) to profile the process
path: profiler_data # Directory where profiling data and plots will be saved
frequency: 0.1 # Sampling frequency (in seconds)
metrics: ["data", "rss", "swap", "uss"]For more information, check Profiler class documentation.
The profiling results are saved as .dat files in the specified output directory and as .png plots showing memory usage trends over time.
Contributions are welcome! If you encounter any issues or have suggestions, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a new branch.
- Make your changes.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by Ghassene Jebali.
