How to fix the “Allowed memory size of ??? bytes exhausted (tried to allocate ??? bytes)” error

While utilizing ElasticPress for your WordPress website to leverage Elasticsearch capabilities, it is common to encounter certain challenges. We will be exploring two prominent issues regarding memory limits that are likely to arise during the syncing process.

Resolving Memory Limit Constraints during Synchronization

One prevalent issue that may arise during ElasticPress implementation involves memory limit constraints during the synchronization process. An indicative error message you might encounter could be:

"Allowed memory size of 536870912 bytes exhausted (tried to allocate 104086464 bytes)"

This issue typically arises when the syncing procedure surpasses the allocated memory limit. Here are a few steps to address this problem:

Investigate why the memory limit was reached

The best way to fix this problem is by understanding why the memory limit was reached. Sometimes there is an infinite loop introduced by a plugin or your theme, or too many posts are being added to memory at once.

If you can’t figure out why the memory limit was exhausted, try lowering the number in the Content Items per Index Cycle field, as described in this article.

Increase the Memory Limit

You can increase the memory limit of your WordPress installation by integrating the following line of code into your wp-config.php file:

define( 'WP_MEMORY_LIMIT', '1024M' );

This line of code instructs the WordPress instance to increase the memory limit to 1024 Megabytes. However, please be aware that this should be done cautiously. Increasing the memory limit will consume more resources from your server, which might affect other processes.

Run a sync through WP CLI

A more efficient way to handle the sync process would be to execute it using WP-CLI (WordPress Command Line Interface). By doing so, you could bypass any potential overheads that might be introduced by the WordPress Dashboard. Check out our What is the ElasticPress Sync? article to read more about it.