A Python library for memory usage profiling and visualization.
Project description
Python Memory Profiler
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.
Features
- 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.
Installation
pip install python-memory-profiler
From source
If 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.txt
Usage
Decorator-Based Profiling
You 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.
Custom Profiling with Profiler Class
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)
Configuration
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.
Output
The profiling results are saved as .dat files in the specified output directory and as .png plots showing memory usage trends over time.
Example Plot
Contributing
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.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Developed by Ghassene Jebali.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_memory_profiler-0.4.4.tar.gz.
File metadata
- Download URL: python_memory_profiler-0.4.4.tar.gz
- Upload date:
- Size: 125.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d43b6dd5aefcf3835bca89a73d2e758c7c5fb248dcbf485708d055fe16d9ce02
|
|
| MD5 |
693fb6dbd66c1868f04ffa827314baa3
|
|
| BLAKE2b-256 |
21179802f567868d79c6baf1d5631a78279507a3bfdba63b83830b1b143cc59a
|
File details
Details for the file python_memory_profiler-0.4.4-py3-none-any.whl.
File metadata
- Download URL: python_memory_profiler-0.4.4-py3-none-any.whl
- Upload date:
- Size: 125.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46b457ec2d23758071a55e716aa9db93b0d4b65046a022139199d4da7ee3b2fc
|
|
| MD5 |
be0555f20fb587fc6665cfb559597ccc
|
|
| BLAKE2b-256 |
3bffa1a2877b30198adbe153bf6ee3e5a8583760fca09779eb6b5d8c80b17e27
|