shipmonk / memory-scanner
Lightweight PHP library for analyzing memory usage, tracking object references, and debugging memory leaks
Requires
- php: ^8.2
Requires (Dev)
- editorconfig-checker/editorconfig-checker: ^10.7.0
- ergebnis/composer-normalize: ^2.47.0
- phpstan/phpstan: ^2.1.14
- phpstan/phpstan-phpunit: ^2.0.6
- phpstan/phpstan-strict-rules: ^2.0.4
- phpunit/phpunit: ^11.5.19
- psr/log: ^3.0
- shipmonk/composer-dependency-analyser: ^1.8.2
- shipmonk/name-collision-detector: ^2.1.1
- shipmonk/phpstan-rules: ^4.1.1
- slevomat/coding-standard: ^8.18
- symfony/browser-kit: ^7.2
- symfony/dependency-injection: ^7.2
- symfony/framework-bundle: ^7.2
- symfony/http-foundation: ^7.2
- symfony/http-kernel: ^7.2
- symfony/routing: ^7.2
This package is auto-updated.
Last update: 2025-05-13 11:13:46 UTC
README
A lightweight PHP library for analyzing memory usage and finding memory leaks in PHP applications.
This package provides tools to:
- Scan PHP memory for objects and their references
- Track object allocations and detect objects that aren't properly deallocated
- Find reference paths from global roots to leaked objects
- Generate detailed explanations of why objects aren't being deallocated
The core functionality relies on traversing the PHP object graph from various memory roots (like superglobals, functions, classes, etc.) to detect what's keeping your objects in memory.
Perfect for debugging memory leaks in long-running PHP applications, especially when working with frameworks or code that maintains complex object relationships.
Usage with Symfony & PHPUnit
In any test that extends KernelTestCase
, you can use the trait ObjectDeallocationCheckerKernelTestCaseTrait
to enable memory leak detection. This requires PHPUnit 11 or higher.
use ShipMonk\MemoryScanner\Bridge\PHPUnit\ObjectDeallocationCheckerKernelTestCaseTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class HomepageControllerTest extends WebTestCase { use ObjectDeallocationCheckerKernelTestCaseTrait; public function testHomepageOk(): void { self::createClient()->request('GET', '/'); self::assertResponseIsSuccessful(); } }
Manual Usage
You can also use the library manually in any PHP script. Here's a simple example:
$objectDeallocationChecker = new \ShipMonk\MemoryScanner\ObjectDeallocationChecker(); $objectDeallocationChecker->expectDeallocation($kernel, 'kernel'); $kernel->shutdown(); unset($kernel); $memoryLeaks = $objectDeallocationChecker->checkDeallocations(); if (count($memoryLeaks) > 0) { Assert::fail($objectDeallocationChecker->explainLeaks($memoryLeaks)); }
Known Limitations
- it's not possible to get list of registered shutdown handlers, so any leak caused by them will be unexplained
- xdebug in 'develop' mode sometimes leaks memory for unknown reasons, but notably 'debug' mode is fine
Installation:
composer require shipmonk/memory-scanner
Contributing
- Check your code by
composer check
- Autofix coding-style by
composer fix:cs
- All functionality must be tested