tourze/symfony-memory-watch-bundle

0.0.1 2025-03-31 00:21 UTC

This package is auto-updated.

Last update: 2025-03-31 00:31:15 UTC


README

English | 中文

Latest Version Total Downloads

A Symfony bundle that monitors and logs memory usage in your application during the request lifecycle.

Features

  • Monitors memory usage between request and response
  • Logs warnings when memory usage exceeds defined thresholds
  • Zero configuration required to get started
  • Works with Symfony's PSR-compatible logging
  • Lightweight with minimal performance impact

Installation

composer require tourze/symfony-memory-watch-bundle

Register the bundle

For Symfony applications using Flex, the bundle should be automatically registered. Otherwise, add it manually to your config/bundles.php:

<?php

return [
    // ...
    Tourze\MemoryWatchBundle\MemoryWatchBundle::class => ['all' => true],
];

Quick Start

The bundle works automatically after installation, monitoring memory usage with a default threshold of 50MB.

You can customize the memory threshold by creating a bundle configuration file:

# config/packages/memory_watch.yaml
services:
  Tourze\MemoryWatchBundle\EventSubscriber\MemoryWatchSubscriber:
    properties:
      memoryThresholdMB: 100 # Set your custom threshold in MB

How It Works

The bundle uses an event subscriber to:

  1. Record memory usage at the beginning of the request
  2. Measure memory usage at the end of the request
  3. Log a warning when the difference exceeds the configured threshold

Memory usage data is collected using PHP's memory_get_usage() function. The bundle uses a WeakMap to associate memory measurements with request objects, ensuring no memory leaks occur.

Requirements

  • PHP 8.1+
  • Symfony 6.4+
  • PSR-compatible logger

Contributing

Contributions are welcome! Feel free to submit a pull request.

License

The MIT License (MIT). Please see License File for more information.