Skip to content

Commit

Permalink
set memory baseline on MemoryCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Sep 15, 2023
1 parent 1bee671 commit 289191a
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/DebugBar/DataCollector/MemoryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -41,14 +50,25 @@ 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
*
* @return integer
*/
public function getPeakUsage()
{
return $this->peakUsage;
return $this->peakUsage - ($this->realUsage ? $this->memoryRealStart : $this->memoryStart);
}

/**
Expand All @@ -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)
);
}

Expand Down

0 comments on commit 289191a

Please sign in to comment.