upscale / stdlib-object-lifecycle
用于监控长时间运行的PHP脚本分配的对象生命周期的库
2.0.3
2019-09-02 06:21 UTC
README
此库跟踪对象在其生命周期中的存在,直至最终销毁。它可以准确地检测长时间运行的CLI脚本或事件驱动的Web服务器(如 Swoole 或 ReactPHP)中的内存泄漏。
对于具有集中对象实例化的应用程序,可以轻松激活监控,即使用DI容器。
特性
- 无需修改源代码即可监控任何对象
- 检测仍然存活的对象
- 检测已销毁的对象
- 存活/已销毁对象的调试信息:类、哈希、跟踪
- 循环引用的垃圾回收
安装
该库应通过 Composer 作为依赖项进行安装
composer require upscale/stdlib-object-lifecycle
用法
检测对象销毁
$obj1 = new \stdClass(); $obj2 = new \stdClass(); $obj3 = new \stdClass(); // Circular references subject to garbage collection $obj1->ref = $obj2; $obj2->ref = $obj1; $watcher = new \Upscale\Stdlib\Object\Lifecycle\Watcher(); $watcher->watch($obj1); $watcher->watch([$obj2, $obj3]); unset($obj1); // Outputs 3 because of circular references echo count($watcher->detectAliveObjects()); unset($obj2); // Outputs 3 because of pending garbage collection echo count($watcher->detectAliveObjects(false)); // Outputs 1 after forced garbage collection echo count($watcher->detectAliveObjects()); unset($obj3); // Outputs 0 and 3 respectively echo count($watcher->detectAliveObjects()); echo count($watcher->detectGoneObjects());
方法 detectAliveObjects()
和 detectGoneObjects()
返回以下调试信息
array( array( 'type' => 'stdClass', 'hash' => '00000000524c32e1000000002cee0034', 'trace' => '#0 demo.php(26): Upscale\\Stdlib\\Object\\Lifecycle\\Watcher->watch(Object(stdClass)) #1 demo.php(10): Example->runTest() #2 demo.php(53): Example->test() #3 {main}', ), ... )
贡献
欢迎提交修复和改进的拉取请求!
许可证
许可协议为 Apache License, Version 2.0。