☰
WORDPRESS / PRACTICE

How to Diagnose PHP Memory Exhaustion in WordPress

How to Diagnose PHP Memory Exhaustion in WordPress

The `Allowed memory size exhausted` error means that a PHP process has reached its configured memory limit. At that point, WordPress cannot complete the current request and terminates it with a fatal error. The cause may not be an underpowered server: memory is often exhausted by a specific plugin, import, backup, complex administrative query, or processing a large number of records.

Proper diagnosis starts not with blindly increasing the limit, but with identifying the context of the problem: does it occur on the public site, in the admin area, during a WP-CLI command, or only in PHP-FPM? The checks below help distinguish a configuration limit from a code issue or excessive workload.

What the PHP Memory Error Actually Means

PHP has a `memory_limit` parameter that restricts the amount of memory available to a single process. If a script tries to allocate more, PHP displays a message such as `Allowed memory size of ... bytes exhausted`. The message usually includes the current limit and the additional amount of memory the process attempted to allocate.

This limit applies to an individual PHP process, not to the entire server. Even if one request stays within the limit, many PHP-FPM processes running at the same time can exhaust physical RAM or cause the server to use swap.

The error may appear only on a specific page or during a particular operation. For example, regular pages may load normally while a product import, a large order editing screen, report generation, or a backup process fails.

  • The PHP limit is lower than what WordPress or active extensions require.
  • A plugin or theme creates a large array, enters an infinite loop, or runs an overly complex query.
  • An import processes all records at once instead of using batches.
  • Different PHP configurations are used in different environments.
  • Too many PHP-FPM processes are running on the server at the same time.

Check PHP Limits in the Correct Context

CLI, PHP-FPM, and web requests may use different PHP versions and different `php.ini` files. Therefore, the result of a terminal command does not always show the limit applied to a browser request.

Start by checking the CLI and WP-CLI environment:

  • Compare the PHP version used by the website with the version used by the command line.
  • Check the path to the active `php.ini` file through the CLI.
  • Find out whether the hosting provider overrides settings at the PHP-FPM pool or control panel level.
  • If the problem occurs only in a browser, check the web configuration rather than relying only on the CLI.
wp eval 'echo ini_get("memory_limit"), PHP_EOL;'
wp --info
php --ini
php -r 'echo ini_get("memory_limit"), PHP_EOL;'

WordPress Memory Limits in wp-config.php

WordPress can request separate memory values for regular and administrative requests. Add the constants to `wp-config.php` before the `/* That's all, stop editing! */` line if they are not already defined:

  • `WP_MEMORY_LIMIT` applies to regular WordPress operations.
  • `WP_MAX_MEMORY_LIMIT` is intended for administrative operations that may require more memory.
  • These constants do not override the system-level `memory_limit`: the effective result depends on the PHP configuration and hosting environment.
  • Choose values based on the server's actual RAM rather than setting the highest value possible.
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');

Find the Failure Point in the Logs

First, determine when the error appeared: after a plugin update, installation of a new theme, a PHP version change, the start of an import, or an increase in the amount of data. Compare the time of the error with entries in the web server, PHP-FPM, and WordPress logs.

The fatal error message often includes a file and line number. This is not always the exact source of the problem: PHP may terminate in WordPress core or a library even though excessive memory usage was caused by the plugin that called that code.

For temporary diagnosis, you can enable WordPress logging in the configuration. On a production site, avoid displaying errors to visitors unless necessary:

  • Check the PHP `error_log` and web server logs.
  • Review PHP-FPM logs if the problem occurs only through web requests.
  • Look for the same URL, AJAX request, cron task, or WP-CLI command immediately before the failure.
  • After the diagnosis, disable detailed debug mode or restrict access to the logs.
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

Check Plugins, the Theme, and Recent Changes

The fastest way to test whether plugins are contributing to the problem is to temporarily deactivate them and repeat the failed operation. If you have console access, WP-CLI lets you do this without logging in to the admin area. Before making changes, record the list of active extensions and make sure you have a backup.

You do not have to deactivate everything on a production site immediately. It is safer to test plugins in groups, starting with extensions that handle imports, products, editors, caching, backups, or reports. If the error disappears after deactivating a group, enable the components one at a time to identify the cause.

If plugins are not responsible, check the theme and functionality in `functions.php`. Pay particular attention to custom filters, cron handlers, REST requests, and code that loads all records without a limit.

  • Check which plugins were updated immediately before the error appeared.
  • Review not only active plugins but also must-use plugins in the `mu-plugins` directory.
  • Compare the behavior on a staging copy of the site.
  • After testing, restore the required plugins and record the result of each check.
wp plugin list --status=active
wp plugin deactivate plugin-slug
wp plugin activate plugin-slug

Optimize Imports and Large Queries

An import often causes memory exhaustion when the code first loads the entire file or all records into one array and only then begins processing. This approach may work with a test dataset but fail on a real site.

Divide the operation into small batches. After processing each batch, release unnecessary variables, avoid accumulating results in a global array, and save progress so the import can resume after an error.

In WordPress queries, avoid unbounded retrieval such as `posts_per_page => -1` when there is a large amount of data. Use pagination, limit fields where possible, and do not run multiple heavy imports in parallel.

For large operations, batch processing through WP-CLI or a job queue is preferable to one long HTTP request. This also reduces the risk of timeouts and reprocessing data that has already been handled.

  • Process CSV, XML, or JSON files in parts instead of loading the entire file into memory.
  • Do not keep all results from previous batches in a variable.
  • Check third-party APIs: a response may contain much more data than you need.
  • Log brief progress information rather than the complete contents of every record.

If the Problem Is Related to PHP-FPM

When the error occurs only while opening the site or the admin area, check the PHP-FPM configuration. The important settings include not only `memory_limit` but also the number of concurrent processes, including `pm.max_children`.

A high memory limit for one process does not mean the server will automatically allocate that amount to every process. However, several heavy requests can consume a significant portion of RAM at the same time. If too many processes are running, the server may start using swap, respond slowly, or terminate processes because of insufficient memory.

Evaluate the settings together with actual RAM usage, the number of CPU cores, the type of workload, and other services such as the database, web server, cron, and backup system. Exact values depend on the server configuration, so change them gradually and monitor graphs and logs.

  • Compare memory usage during regular traffic with usage during the problematic operation.
  • Check for large numbers of simultaneous cron tasks or imports.
  • Do not increase `pm.max_children` if the server is already close to its RAM limit.
  • After making changes, restart PHP-FPM only according to your hosting provider's procedures and verify the result.

Common Mistakes When Fixing the Problem

Increasing `WP_MEMORY_LIMIT` may temporarily bring the site back online, but it does not fix a memory leak, an inefficient query, or a plugin issue. If the underlying cause remains, the problem will return as the amount of data or the number of parallel requests increases.

Do not edit an arbitrary `php.ini` file without checking which file is used by the relevant context. It is equally unsafe to add multiple conflicting `WP_MEMORY_LIMIT` definitions in different parts of the configuration.

Do not limit testing to the homepage: test the exact action that triggers the error. After applying a fix, repeat the import, open the problematic screen, and check the logs to make sure the error has not simply changed form.

  • Do not set an unjustifiably high memory limit.
  • Do not delete log entries before identifying the cause.
  • Do not deactivate all plugins without a recovery plan and a backup.
  • Do not confuse the limit for one PHP process with the server's total RAM.
  • Do not run heavy operations simultaneously in the browser, cron, and WP-CLI.

Frequently asked questions

Is simply increasing memory_limit enough?

No. It may be a temporary measure, but you should first check the logs, plugins, imports, and actual workload. If the code has a memory leak or loads all data into memory, the problem will return.

Why does WP-CLI show a different memory limit than the website?

CLI and web requests may use different PHP versions, `php.ini` files, or PHP-FPM settings. Check each context separately.

Can I set WP_MEMORY_LIMIT to 1G?

Technically, this depends on the server configuration, but setting such a value without analyzing RAM and the number of PHP processes is unsafe. It may allow one request to consume too much memory and degrade the entire server.

How can I find the plugin causing the error?

Record the active plugins, temporarily deactivate them in groups through WP-CLI or another safe method, and repeat the problematic action. Once you identify the problematic group, enable the plugins one at a time.

Conclusion

Diagnosing the `Allowed memory size exhausted` error should begin by identifying the context: a web request, PHP-FPM, CLI, or a specific administrative operation. Then check the limits, logs, and changes that preceded the failure.

Increasing the memory limit is appropriate only as part of a broader solution. Stable results come from using batch imports, controlling large queries, finding the problematic plugin or code, and aligning PHP-FPM settings with the server's actual RAM.

← All WordPress materials