tourze / symfony-memory-watch-bundle
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/symfony-memory-watch-bundle
Requires
- php: ^8.1
- psr/log: ^3|^2|^1
- symfony/framework-bundle: ^6.4
This package is auto-updated.
Last update: 2025-10-31 20:05:05 UTC
README
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
After installation, the bundle is ready to use with zero configuration. It will automatically:
- Monitor memory usage - Track memory consumption for each request
- Log warnings - Alert when memory usage exceeds 50MB threshold
- Clean up automatically - Use WeakMap to prevent memory leaks
The bundle logs to your application's default logger when memory usage exceeds the threshold. Check your logs to see output like:
[2024-01-01 12:00:00] app.WARNING: 内存使用超过阈值 {"使用内存":75.5,"内存阈值":"50MB"}
No additional configuration is required - the bundle works out of the box.
How It Works
The bundle uses an event subscriber to:
- Record memory usage at the beginning of the request
- Measure memory usage at the end of the request
- 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.