diff --git a/src/DebugBar/DataCollector/MemoryCollector.php b/src/DebugBar/DataCollector/MemoryCollector.php index 1a34f3bd..8d9e75a7 100644 --- a/src/DebugBar/DataCollector/MemoryCollector.php +++ b/src/DebugBar/DataCollector/MemoryCollector.php @@ -17,8 +17,17 @@ class MemoryCollector extends DataCollector implements Renderable { protected $realUsage = false; + protected $memoryRealStart = 0; + + protected $memoryStart = 0; + protected $peakUsage = 0; + public function __construct() + { + $this->updateMemoryBaseline(); + } + /** * Returns whether total allocated memory page size is used instead of actual used memory size * by the application. See $real_usage parameter on memory_get_peak_usage for details. @@ -41,6 +50,17 @@ public function setRealUsage($realUsage) $this->realUsage = $realUsage; } + /** + * Set memory baseline for avoid problems on swoole/octane + * + * @return void + */ + public function updateMemoryBaseline() + { + $this->memoryStart = memory_get_usage(false); + $this->memoryRealStart = memory_get_usage(true); + } + /** * Returns the peak memory usage * @@ -48,7 +68,7 @@ public function setRealUsage($realUsage) */ public function getPeakUsage() { - return $this->peakUsage; + return $this->peakUsage - ($this->realUsage ? $this->memoryRealStart : $this->memoryStart); } /** @@ -66,8 +86,8 @@ public function collect() { $this->updatePeakUsage(); return array( - 'peak_usage' => $this->peakUsage, - 'peak_usage_str' => $this->getDataFormatter()->formatBytes($this->peakUsage, 0) + 'peak_usage' => $this->getPeakUsage(), + 'peak_usage_str' => $this->getDataFormatter()->formatBytes($this->getPeakUsage(), 0) ); }