All Toolsโ€บMemory Usage Profiler
๐Ÿ”ง Generative AI AdvancementsMay 11, 2026โœ… Tests passing

Memory Usage Profiler

This library profiles memory consumption patterns of generative AI models during inference by tracking memory usage over time, as well as for specific model calls. Developers can use it to identify potential memory bottlenecks, optimize infrastructure, and understand resource requirements for deploying their models.

What It Does

  • Profiles real-time memory usage during function execution.
  • Generates detailed memory usage reports over time.
  • Creates a time-series graph of memory usage trends.

Installation

To install the required dependencies, run:

pip install psutil==5.9.5 matplotlib==3.8.0

Usage

Example

from memory_usage_profiler import profile_memory

@profile_memory
def generate_text(model, input_text):
    # Simulate model inference
    result = model.generate(input_text)
    return result

# Example function usage
def example_function():
    data = []
    for i in range(1000000):
        data.append(i)
    return sum(data)

profiled_example = profile_memory(example_function)
profiled_example()

CLI Usage

To run the example function with memory profiling:

python memory_usage_profiler.py --example

This will print memory usage over time to the console and generate a PNG graph of memory usage trends.

Source Code

import psutil
import matplotlib.pyplot as plt
import time
import functools
from typing import Callable

def profile_memory(func: Callable):
    """
    A decorator to profile memory usage during the execution of a function.
    Logs memory usage over time and optionally generates a memory usage graph.
    
    Args:
        func (Callable): The function to be profiled.
    
    Returns:
        Callable: The wrapped function with memory profiling.
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        memory_usage = []
        timestamps = []

        def monitor():
            """Monitor memory usage and record it."""
            process = psutil.Process()
            while not stop_monitoring:
                memory_usage.append(process.memory_info().rss / (1024 * 1024))  # Convert to MB
                timestamps.append(time.time() - start_time)
                time.sleep(0.1)  # Sample memory every 100ms

        # Start monitoring in a separate thread
        import threading
        stop_monitoring = False
        start_time = time.time()
        monitor_thread = threading.Thread(target=monitor)
        monitor_thread.start()

        try:
            result = func(*args, **kwargs)
        finally:
            stop_monitoring = True
            monitor_thread.join()

        # Log memory usage
        print(f"Memory usage during '{func.__name__}':")
        for t, mem in zip(timestamps, memory_usage):
            print(f"Time: {t:.2f}s, Memory: {mem:.2f} MB")

        # Generate memory usage graph
        plt.figure(figsize=(10, 6))
        plt.plot(timestamps, memory_usage, label='Memory Usage (MB)')
        plt.xlabel('Time (s)')
        plt.ylabel('Memory Usage (MB)')
        plt.title(f'Memory Usage Profile for {func.__name__}')
        plt.legend()
        plt.grid()
        plt.savefig(f"{func.__name__}_memory_profile.png")
        plt.close()

        return result

    return wrapper

if __name__ == "__main__":
    import argparse

    def example_function():
        """Example function to demonstrate memory profiling."""
        data = []
        for i in range(1000000):
            data.append(i)
        return sum(data)

    parser = argparse.ArgumentParser(description="Memory Usage Profiler")
    parser.add_argument('--example', action='store_true', help="Run the example function with memory profiling.")
    args = parser.parse_args()

    if args.example:
        profiled_example = profile_memory(example_function)
        profiled_example()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
memory_usage_profiler
Category
Generative AI Advancements
Generated
May 11, 2026
Tests
Passing โœ…

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-05-11/memory_usage_profiler
cd generated_tools/2026-05-11/memory_usage_profiler
pip install -r requirements.txt 2>/dev/null || true
python memory_usage_profiler.py
Memory Usage Profiler โ€” AI Tools by AutoAIForge