Skip to content

Commit ba8bf73

Browse files
james-johnston-thumbtackbarryvdh
authored andcommitted
Show memory usage actually allocated by user PHP code (php-debugbar#321)
The memory_get_usage and memory_get_peak_usage functions have a $real_usage parameter. Update the MemoryCollector and PDOCollector collectors to use a value of false instead of true. For MemoryCollector, we allow the user to choose which type of memory usage to gather. To recap, when $real_usage is set (and previously this was the hard-coded default), PHP returns the amount of memory allocated from the operating system by the PHP memory manager. This is typically done in large chunks of memory and is not very granular. When $real_usage is cleared, PHP returns the amount of memory the user’s application has actually allocated from the PHP memory manager. Clearing $real_usage has useful application especially for the PDOCollector. As an example, a query that returns only a few rows will often say that zero bytes were used when $real_usage is set. That’s because PHP was able to satisfy the memory requests from memory it had already allocated from the operating system. To contrast, the same query will show an expected few kilobytes of memory usage when $real_usage is set to false.
1 parent 5fc3605 commit ba8bf73

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/DebugBar/DataCollector/MemoryCollector.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,32 @@
1515
*/
1616
class MemoryCollector extends DataCollector implements Renderable
1717
{
18+
protected $realUsage = false;
19+
1820
protected $peakUsage = 0;
1921

22+
/**
23+
* Returns whether total allocated memory page size is used instead of actual used memory size
24+
* by the application. See $real_usage parameter on memory_get_peak_usage for details.
25+
*
26+
* @return bool
27+
*/
28+
public function getRealUsage()
29+
{
30+
return $this->realUsage;
31+
}
32+
33+
/**
34+
* Sets whether total allocated memory page size is used instead of actual used memory size
35+
* by the application. See $real_usage parameter on memory_get_peak_usage for details.
36+
*
37+
* @param bool $realUsage
38+
*/
39+
public function setRealUsage($realUsage)
40+
{
41+
$this->realUsage = $realUsage;
42+
}
43+
2044
/**
2145
* Returns the peak memory usage
2246
*
@@ -32,7 +56,7 @@ public function getPeakUsage()
3256
*/
3357
public function updatePeakUsage()
3458
{
35-
$this->peakUsage = memory_get_peak_usage(true);
59+
$this->peakUsage = memory_get_peak_usage($this->realUsage);
3660
}
3761

3862
/**

src/DebugBar/DataCollector/PDO/TracedStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct($sql, array $params = array(), $preparedId = null)
4646
public function start($startTime = null, $startMemory = null)
4747
{
4848
$this->startTime = $startTime ?: microtime(true);
49-
$this->startMemory = $startMemory ?: memory_get_usage(true);
49+
$this->startMemory = $startMemory ?: memory_get_usage(false);
5050
}
5151

5252
/**
@@ -59,7 +59,7 @@ public function end(\Exception $exception = null, $rowCount = 0, $endTime = null
5959
{
6060
$this->endTime = $endTime ?: microtime(true);
6161
$this->duration = $this->endTime - $this->startTime;
62-
$this->endMemory = $endMemory ?: memory_get_usage(true);
62+
$this->endMemory = $endMemory ?: memory_get_usage(false);
6363
$this->memoryDelta = $this->endMemory - $this->startMemory;
6464
$this->exception = $exception;
6565
$this->rowCount = $rowCount;

0 commit comments

Comments
 (0)