Diagnose persistent memory growth by distinguishing live object references, cyclic garbage and allocator-reserved memory in long-running PHP processes.
What It Does
Measure request-local memory and elapsed time without assuming a leak. Distinguish live references from allocator-reserved memory before blaming a leak.
Requirements and Compatibility
Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.
Syntax, Inputs and Results
The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.
Example 1: Basic Use
Measure request-local memory and elapsed time without assuming a leak.
<?php
$start = hrtime(true);
$data = array_fill(0, 10000, 2);
echo 'Bytes: ', memory_get_peak_usage(true), "\n";
echo 'Milliseconds: ', round((hrtime(true) - $start) / 1e6, 2);
Example 2: Another Case
Distinguish live references from allocator-reserved memory before blaming a leak.
<?php
$records = [];
for ($i = 0; $i < 100; $i++) {
$records[] = str_repeat('x', 200);
}
unset($records);
gc_collect_cycles();
echo memory_get_usage(true);
Practical Example
Log structure without logging passwords or access tokens.
<?php
$user = ['id' => 42, 'active' => true];
var_dump($user);
error_log('User type: ' . get_debug_type($user));
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Avoid displaying stack traces, user data or credentials in public HTTP responses.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| Step debugging vs profiling | Step debugging pauses individual statements; profiling records aggregate runtime cost. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Enabling the debugger for every production request | Use controlled triggers and disable development modes in production. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | Avoid displaying stack traces, user data or credentials in public HTTP responses. |
Key Takeaways
- Diagnose persistent memory growth by distinguishing live object references, cyclic garbage and allocator-reserved memory in long-running PHP processes.
- Avoid displaying stack traces, user data or credentials in public HTTP responses.
- Keep application configuration, source code and validation tests under version control.
Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.