Skip to main content

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 data folder.
  • 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

Memory Usage Example

Contributing

Contributions are welcome! If you encounter any issues or have suggestions, feel free to open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_memory_profiler-0.4.4.tar.gz (125.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_memory_profiler-0.4.4-py3-none-any.whl (125.0 kB view details)

Uploaded Python 3

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

Hashes for python_memory_profiler-0.4.4.tar.gz
Algorithm Hash digest
SHA256 d43b6dd5aefcf3835bca89a73d2e758c7c5fb248dcbf485708d055fe16d9ce02
MD5 693fb6dbd66c1868f04ffa827314baa3
BLAKE2b-256 21179802f567868d79c6baf1d5631a78279507a3bfdba63b83830b1b143cc59a

See more details on using hashes here.

File details

Details for the file python_memory_profiler-0.4.4-py3-none-any.whl.

File metadata

File hashes

Hashes for python_memory_profiler-0.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 46b457ec2d23758071a55e716aa9db93b0d4b65046a022139199d4da7ee3b2fc
MD5 be0555f20fb587fc6665cfb559597ccc
BLAKE2b-256 3bffa1a2877b30198adbe153bf6ee3e5a8583760fca09779eb6b5d8c80b17e27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page