What Happens to PHP Memory During One Request?
See when PHP values share storage, become eligible for cleanup, and count toward a script’s memory limit, with a small current-versus-peak example.
When a PHP script creates a value, PHP manages its memory automatically. Assigning an array to another variable need not immediately duplicate its storage: the variables can share it until one changes. As references disappear, values can become eligible for cleanup, although a reference cycle needs a different kind of collection. All the while, memory_limit caps what that script may allocate, so an allocation can fail before the script finishes.
Follow allocations through one request
Imagine a web request whose PHP script creates an array, assigns it to $copy, and then adds an item to $copy. PHP’s copy-on-write behavior lets the two variables share memory after the assignment. The write is the important turn: changing one copy can require separate storage so the other value remains unchanged. An assignment is therefore not, by itself, evidence that the array’s storage has doubled. A later write may have a different memory consequence.
This is automatic memory management, not a sequence of allocations and frees the script author must write by hand. The useful question when tracing the script is which values still have references, and which operation changes a shared value? The copy-on-write explanation describes sharing until a variable changes.
The script’s included files do not get fresh memory allowances. Code brought in with require or include runs under the parent script’s limit. An included file may create the value that matters, but it is still part of this script’s allocation story.
Explain reclamation: no references versus a cycle
Reference counting covers the straightforward case. If a value still has a reference, removing one variable name does not necessarily make that value unnecessary. Once its reference count reaches zero, PHP can free its memory. In the array scenario, unsetting $copy leaves $items in place; unsetting both removes those variable references. That explains eligibility for cleanup, not a promised drop of a particular number of bytes at the next measurement.
A cycle poses a different problem. Picture two values that refer to each other, with no remaining reference to either from the rest of the script. Their mutual references prevent the simple “count reaches zero” explanation from resolving the pair. PHP combines reference counting with a cycle-collecting garbage collector for this case, as described in the memory management guide. Zero-reference cleanup and cycle collection are related parts of the model, not interchangeable names for the same event.
Check the model with one array
This self-contained example labels both measurements at each stage. It does not assume an exact byte difference, and the numbers will depend on the run. The values report usage for the script at each call, not the exact size of $items or $copy.
<?php
function showMemory(string $stage): void
{
echo $stage,
': current=', memory_get_usage(), ' bytes',
'; peak=', memory_get_peak_usage(), " bytes\n";
}
showMemory('Before array');
$items = ['red', 'blue', 'green', 'yellow', 'orange', 'purple'];
showMemory('After creating array');
$copy = $items;
showMemory('After assignment');
$copy[] = 'white';
showMemory('After writing to copy');
unset($copy);
showMemory('After unsetting copy');
unset($items);
showMemory('After unsetting original');
Read the current figure as usage at the labeled point and the peak as the highest usage recorded so far. Assignment may leave the readings close together because storage can be shared. The write can change usage because it changes a copy. Unsetting a variable may lower current usage when memory becomes eligible for cleanup, while peak still records an earlier high. A published unset example shows that combination, but its byte counts are observations from that example, not expected output for this one.
Put the limit and the request boundary in place
The php.ini directive can be written as:
memory_limit = 128M
That sets a cap on memory a single script may allocate. It includes work done by its included files, but it is not one shared 128M pool for every concurrent script. Several scripts can each be below their own limit while their combined memory use exceeds that figure. Conversely, one script can encounter an allowed-memory-size error on an allocation before reaching the end of its work. The per-script limit explanation makes both distinctions.
For a web request, the end of its script is the boundary of the lifecycle traced here. The example instead runs as a standalone script if invoked through CLI; CLI PHP may read a different php.ini from the web environment. Compare configuration only in the environment whose behavior you are trying to explain. Nothing in the current and peak readings establishes how much memory PHP returns to the operating system when the script ends.
