Skip to main content

๐Ÿ” Modular Python Memory Monitoring & Valgrind Integration - Like Lego for your Code

Project description

๐Ÿ” MemoryGuard

Like Lego for your Code - Modular Python Memory Monitoring & Valgrind Integration

Tests Coverage Docker PyPI Python License: MIT Docker

Drop-in memory monitoring for any Python project. Track memory usage, detect leaks, profile tools, and integrate with Valgrind - all with minimal overhead.

โœจ Features

  • ๐ŸŽฏ Zero Config - Works out of the box
  • ๐Ÿ“Š Real-time Monitoring - Live memory tracking
  • ๐Ÿ” Leak Detection - Automatic leak identification
  • ๐Ÿ› ๏ธ Tool Profiling - Per-function memory analysis
  • ๐Ÿ“บ Live Dashboard - Beautiful terminal UI (optional)
  • ๐Ÿ”ง Valgrind Integration - Deep C-extension analysis
  • ๐Ÿš€ CI/CD Ready - GitHub Actions integration
  • ๐Ÿงฉ Modular - Use only what you need

๐Ÿš€ Quick Start

pip install memoryguard

Basic Usage

from memoryguard import MemoryGuard, track_memory

# Initialize
guard = MemoryGuard(threshold_mb=500)

# Track any function
@track_memory("heavy_computation", guard)
def heavy_computation():
    return sum(i**2 for i in range(1000000))

result = heavy_computation()

# Check memory manually
alert = guard.check_memory("after_computation")
if alert:
    print(f"โš ๏ธ  {alert.message}")

Context Manager

from memoryguard import memory_context

with memory_context("database_query", guard):
    # Your code here
    large_dataset = fetch_data()
    process(large_dataset)
# Memory automatically tracked

Live Dashboard

from memoryguard import MemoryDashboard

dashboard = MemoryDashboard(guard)

with dashboard.live_display():
    # Your long-running operation
    run_big_analysis()

๐Ÿงฉ Integration Patterns

Pattern 1: Decorator (Simplest)

from memoryguard import track_memory, get_memory_guard

guard = get_memory_guard(threshold_mb=1000)

@track_memory("my_function", guard)
def my_function():
    pass

Pattern 2: Class Inheritance (For Tools)

from memoryguard import MemoryInstrumentedTool

class MyScanner(MemoryInstrumentedTool):
    tool_name = "myscanner"
    
    def scan(self, target):
        with self.memory_context(target):
            return self.do_scan(target)

Pattern 3: Orchestrator (For Workflows)

from memoryguard import MemoryAwareOrchestrator

orchestrator = MemoryAwareOrchestrator(max_memory_mb=2000)
orchestrator.register_tool(my_tool)

result = await orchestrator.execute_with_memory_control(
    "my_tool", target, my_tool.scan(target)
)

๐Ÿ“Š Dashboard

If you have rich installed:

pip install memoryguard[dashboard]

Live Dashboard Preview

MemoryGuard Dashboard

Live memory monitoring with top processes, RSS/VMS usage, and memory percentage

from memoryguard import MemoryDashboard

dashboard = MemoryDashboard(guard)

# Simple text output
print(dashboard.get_simple_display())
# [Memory: 456.3MB | Status: OK | Alerts: 0]

# Or live display
with dashboard.live_display():
    run_analysis()

Shows:

  • Live memory graph (sparkline)
  • RSS/VMS usage
  • Recent alerts
  • Tool breakdown
  • Progress bars

๐Ÿ”ง Valgrind Integration

For C-extension analysis:

from memoryguard import ValgrindWrapper

wrapper = ValgrindWrapper()

if wrapper.available:
    result = wrapper.check_python_module(
        "my_c_extension",
        timeout=300
    )
    print(result['valgrind_log'])

๐Ÿš€ CI/CD Integration

GitHub Actions

name: Memory Check

on: [push, pull_request]

jobs:
  memory-test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    
    - name: Install
      run: pip install memoryguard
    
    - name: Run with Memory Tracking
      run: |
        python -c "
        from memoryguard import MemoryGuard
        guard = MemoryGuard()
        # ... your code ...
        guard.generate_report('memory-report.json')
        "
    
    - name: Upload Report
      uses: actions/upload-artifact@v3
      with:
        name: memory-report
        path: memory-report.json

๐Ÿ“ˆ Advanced Usage

Memory-Efficient Batch Processing

from memoryguard import memory_efficient_batch

async def process_targets(targets):
    async def scan_batch(batch):
        return [await scan(t) for t in batch]
    
    return await memory_efficient_batch(
        targets,
        scan_batch,
        batch_size=10
    )

Leak Detection

# Reset baseline
guard.reset_baseline()

# Run your code
suspected_leaky_function()

# Check for leaks
leak_report = guard.detect_leaks("my_function")
if leak_report:
    print(f"Leaked: {leak_report['total_leaked_mb']}MB")

Force Garbage Collection

stats = guard.force_gc()
print(f"Freed {stats['memory_freed_mb']:.1f}MB")
print(f"Collected {stats['objects_collected']} objects")

โš™๏ธ Configuration

guard = MemoryGuard(
    threshold_mb=500,           # Warning threshold
    critical_threshold_mb=1000,  # Critical threshold  
    check_interval=30,           # Background check interval (seconds)
    enable_snapshots=True,       # Store history
    log_dir="~/.memoryguard/logs"  # Report location
)

๐Ÿงช Testing

# Install dev dependencies
pip install memoryguard[dev]

# Run tests
pytest tests/ -v

# With coverage
pytest tests/ --cov=memoryguard

# Benchmarks
pytest tests/ --benchmark-only

๐Ÿ“ Project Structure

memoryguard/
โ”œโ”€โ”€ memoryguard/
โ”‚   โ”œโ”€โ”€ __init__.py          # Main exports
โ”‚   โ”œโ”€โ”€ core.py              # MemoryGuard, Valgrind
โ”‚   โ”œโ”€โ”€ integration.py       # Tool integration
โ”‚   โ””โ”€โ”€ dashboard.py         # TUI (requires rich)
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_memoryguard.py
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ demo.py
โ”œโ”€โ”€ pyproject.toml
โ””โ”€โ”€ README.md

๐Ÿค Usage in Other Projects

As Git Submodule

git submodule add https://github.com/SHAdd0WTAka/memoryguard.git third_party/memoryguard

As Local Package

# Clone anywhere
git clone https://github.com/SHAdd0WTAka/memoryguard.git

# Install in editable mode
pip install -e ./memoryguard

Copy-Paste (Single File)

Just copy memoryguard/core.py to your project - it's self-contained!

๐Ÿ“ Requirements

  • Python 3.8+
  • psutil
  • Optional: rich (for dashboard)
  • Optional: valgrind (for C analysis)

๐Ÿ“„ License

MIT License - see LICENSE file

๐Ÿ™ Credits

Created for CLAWDBOT / Zen-AI-Pentest - now available for everyone!


Like Lego for your Code ๐Ÿ”ง๐Ÿงฑ - Just drop it in and go!

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

memoryguard-1.0.2.tar.gz (122.1 kB view details)

Uploaded Source

Built Distribution

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

memoryguard-1.0.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file memoryguard-1.0.2.tar.gz.

File metadata

  • Download URL: memoryguard-1.0.2.tar.gz
  • Upload date:
  • Size: 122.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for memoryguard-1.0.2.tar.gz
Algorithm Hash digest
SHA256 3ebc41894ceb11677f0d0c5ac5c526c2bf5f2c9df284cbd50fbaa0a17f528b88
MD5 67bf5507b8d4b02724b91ebec3e67485
BLAKE2b-256 ad13cd593e71ea73a7ae612dde3e11702c565a71ff4bd8e7c482ae570d83bd5a

See more details on using hashes here.

File details

Details for the file memoryguard-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: memoryguard-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for memoryguard-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1fb10d0a744ba9bce651ba6440c95317d48829dfbcb6e1304ad0e8e9bb87fca0
MD5 3dd0cbfbf5afaf2a730b98af328d6a50
BLAKE2b-256 3aaeb0f4ae9819f42fb7fcd2579a8ceacf40da5569bc2947c8654dcbad72d635

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