PHP Memory Limit Explained: Configuration, Best Practices, and Troubleshooting

Published: 2026-07-06
Google AdSense 广告位 (待申请通过后开启)

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

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

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.

Request
   │
   ▼
PHP Script
   │
Memory Usage
   │
   ├── 50 MB
   ├── 120 MB
   ├── 200 MB
   └── 256 MB (Limit Reached)
           │
           ▼
Fatal Error

Once the configured limit is exceeded, PHP stops the script to protect the server.

Memory Limit Reference Table

| Value |                           Memory |
| ----- | -------------------------------- |
|   64M |                    Small scripts |
|  128M |                   Basic websites |
|  256M | Most production PHP applications |
|  512M |    Large frameworks and Composer |
|    1G |            Heavy data processing |

Check the Current Memory Limit

From the command line:

php -i | grep memory_limit

Example output:

memory_limit => 256M => 256M

Or:

php -r "echo ini_get('memory_limit');"

Locate php.ini

Find the active configuration file.

php --ini

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.

sudo nano /etc/php.ini

Locate:

memory_limit = 128M

Change it to:

memory_limit = 256M

Save the file.

Restart PHP-FPM

Reload the configuration.

sudo systemctl restart php-fpm

Verify that the service starts successfully.

sudo systemctl status php-fpm

Expected:

Active: active (running)

Recommended Values

There is no single correct value for every application.

| Application           | Recommended Value |
| --------------------- | ----------------- |
| Small PHP website     |              128M |
| WordPress             |              256M |
| Laravel               |         256M–512M |
| Composer              |    512M or higher |
| Large data processing |           512M–1G |

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:

php -i | grep memory_limit

Or create a simple PHP file:

<?php
phpinfo();

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

Technology Guides →

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

Southeast Asia Insights →