memory_limit caps PHP-managed allocation per request or CLI process, not total FPM pool RAM.
What It Does and When to Use It
Read the active per-process PHP memory limit. Current allocations answer how much PHP holds now; peak allocations expose workload spikes that can exceed the process limit.
Requirements, Inputs and Expected Behavior
Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Raising it without calculating worker concurrency can destabilize the host.
Example 1: Basic Operation
Read the active per-process PHP memory limit.
<?php
$limit=ini_get('memory_limit');
echo $limit==='-1'?'Unlimited by PHP':"PHP memory_limit = $limit";
Example 2: Behavior and Validation
Distinguish current memory from peak memory reported by PHP.
<?php
$values=range(1,10000);
echo 'Current: ',memory_get_usage(true),' Peak: ',memory_get_peak_usage(true);
Practical Example: Application Use
Budget batch sizes instead of increasing memory_limit without measuring the workload.
<?php
$bytes=32*1024*1024;
$rows=(int)($bytes/1024);
echo 'Target row budget: ', $rows;
Important Behavior and Edge Cases
The second example highlights: Distinguish current memory from peak memory reported by PHP. The practical example adds: Budget batch sizes instead of increasing memory_limit without measuring the workload.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| memory_get_usage versus memory_get_peak_usage | Current allocations answer how much PHP holds now; peak allocations expose workload spikes that can exceed the process limit. |
| Choosing safely | Measure the representative workload before changing memory_limit or forcing garbage collection. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming successful execution proves the result is usable | Check the return value, required extension, and observable output of the specific operation. |
| Ignoring runtime and environment differences | Verify PHP version, SAPI, operating system, service access, and permissions before deployment. |
| Skipping operational constraints | Raising it without calculating worker concurrency can destabilize the host. |
Key Takeaways
- memory_limit caps PHP-managed allocation per request or CLI process, not total FPM pool RAM.
- Budget batch sizes instead of increasing memory_limit without measuring the workload.
- Measure the representative workload before changing memory_limit or forcing garbage collection.