PHP Memory Limit Explained: Configuration, Best Practices, and Troubleshooting
Introduction
The memory_limit directive is one of the most important PHP configuration options. It defines the maximum amount of memory a single PHP script is allowed to consume during execution.
Setting an appropriate memory limit helps prevent individual scripts from exhausting server resources while ensuring that applications have enough memory to complete their tasks.
This guide explains how PHP memory limits work, how to configure them, recommended values for different workloads, and how to troubleshoot common memory-related errors.
Test Environment
What Is PHP memory_limit?
The memory_limit directive specifies the maximum amount of memory that a single PHP script can allocate.
For example:
memory_limit = 256M
If a script attempts to allocate more than 256 MB, PHP immediately terminates execution and returns a fatal error.
This prevents a single request from consuming all available server memory.
How memory_limit Works
When a PHP request starts, it is given a memory budget.
Once the configured limit is exceeded, PHP stops the script to protect the server.
Memory Limit Reference Table
Check the Current Memory Limit
From the command line:
Example output:
memory_limit => 256M => 256M
Or:
Locate php.ini
Find the active configuration file.
Example:
Loaded Configuration File: /etc/php.ini
For PHP-FPM installations, make sure you edit the configuration file used by PHP-FPM rather than only the CLI version.
Change the Memory Limit
Open the configuration file.
Locate:
memory_limit = 128M
Change it to:
memory_limit = 256M
Save the file.
Restart PHP-FPM
Reload the configuration.
Verify that the service starts successfully.
Expected:
Active: active (running)
Recommended Values
There is no single correct value for every application.
Increasing the limit should only be done when the application genuinely requires additional memory.
Common Fatal Error
One of the most common PHP errors is:
Fatal error:
Allowed memory size of 268435456 bytes exhausted
This indicates that the script exceeded the configured memory limit.
The number shown in bytes corresponds to the configured limit.
For example:
134217728 bytes = 128 MB
268435456 bytes = 256 MB
536870912 bytes = 512 MB
Should You Set memory_limit to -1?
PHP also supports:
memory_limit = -1
This removes the memory limit entirely.
Although it may seem convenient, it is generally not recommended on production servers. A faulty script or infinite loop could consume all available memory, potentially affecting other applications or causing the operating system to terminate processes.
Why Increasing memory_limit Is Not Always the Solution
Many developers simply increase the limit whenever they encounter a memory error.
However, excessive memory usage may indicate:
Loading large datasets into memory.
Memory leaks in application code.
Inefficient database queries.
Processing files that should be streamed instead of fully loaded.
Poor algorithm design.
Before increasing the limit, identify why the application requires so much memory.
Verify the New Value
Run:
Or create a simple PHP file:
Search for:
memory_limit
The displayed value should match the updated configuration.
Common Issues
Changes Have No Effect
Possible causes include:
PHP-FPM was not restarted.
The wrong php.ini file was modified.
PHP CLI and PHP-FPM are using different configuration files.
Different Values in CLI and Browser
This is normal if the CLI and PHP-FPM use separate configuration files.
Verify both environments before troubleshooting.
Application Still Reports Memory Errors
The application may override memory_limit at runtime or require additional optimization beyond increasing the limit.
Best Practices
Use the smallest value that meets your application's requirements.
Avoid setting memory_limit = -1 on production servers.
Monitor memory usage before increasing the limit.
Investigate memory-intensive code instead of relying solely on higher limits.
Restart PHP-FPM after changing configuration values.
Conclusion
The memory_limit directive protects your server by limiting the amount of memory each PHP script can consume. Choosing an appropriate value improves server stability while giving applications enough resources to run efficiently.
Rather than simply increasing the limit whenever an error occurs, monitor your application's memory usage and optimize inefficient code where possible. This approach results in better performance, lower resource consumption, and a more reliable production environment.
Explore More
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices
› PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices
› PHP opcache.enable Explained: Configuration, Performance, and Best Practices