Troubleshooting

Fix "Allowed memory size exhausted" PHP errors

Understand the PHP fatal error 'Allowed memory size of X bytes exhausted', raise memory_limit on cPanel or a VPS, and find the code that uses too much memory.

BeginnercPanel hosting · Linux VPS3 min read

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) means a script tried to use more memory than PHP's memory_limit allows. The number is the limit in bytes (134217728 = 128 MB). This guide shows how to fix the allowed memory size exhausted error properly, not just by raising the limit forever.

Before you start

  • Find the full error in the site's error_log or PHP log: it names the file and line.
  • Know where your site runs: cPanel hosting or your own VPS.

Read the error

Bytes in messageLimit
6710886464 MB
134217728128 MB
268435456256 MB
536870912512 MB

The file and line show where memory ran out, which is often a plugin, an image library or a large database query, but not always the code that used most of it.

Raise memory_limit on cPanel hosting

  1. Open cPanel → Software → Select PHP Version (or MultiPHP INI Editor).
  2. Go to the Options tab.
  3. Set memory_limit to 256M (or 512M for heavy stores) and save.

Your plan's CloudLinux limits still apply on top of this: a script cannot use more RAM than the account's physical memory limit.

Raise memory_limit on a VPS

Find the php.ini that the web server uses (via a phpinfo() page, Loaded Configuration File), e.g. /etc/php/8.3/fpm/php.ini, and set:

memory_limit = 256M

Restart PHP-FPM: systemctl restart php8.3-fpm. For one site only, set it in the pool file: php_admin_value[memory_limit] = 256M. The CLI uses a separate file (/etc/php/8.3/cli/php.ini), relevant for Composer and cron scripts.

Per-folder overrides

  • .user.ini in the site root (PHP-FPM, LSAPI): memory_limit = 256M
  • .htaccess (Apache with mod_php only): php_value memory_limit 256M. On other handlers this line causes a 500 error.
  • In code: ini_set('memory_limit', '256M');

WordPress-specific limits

WordPress has its own constants in wp-config.php, above the "stop editing" line:

define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');

These cannot exceed the server's memory_limit. Check the effective value in Tools → Site Health → Info → Server.

Find the real cause

If the error returns at 256 MB or 512 MB, something is wrong in the code:

  • A plugin or module: disable plugins one by one (or rename the plugins folder) and retest.
  • Huge queries: code loading thousands of rows or posts at once. Process in batches.
  • Image processing: very large uploaded images need lots of RAM to resize. Resize before upload.
  • Infinite loops or recursion: the same file and line appear every time.
  • Imports/exports: run them via WP-CLI or the command line, where you can raise the limit for that one run: php -d memory_limit=1G script.php.

Check the fix

  • Reload the page or rerun the task that failed.
  • Confirm the new value with phpinfo() (delete the file after) or php -i | grep memory_limit for the CLI.

Troubleshooting

  • Value does not change: you edited the wrong php.ini, or a later file overrides it. phpinfo() shows the active value and loaded ini files.
  • 500 error after editing .htaccess: remove the php_value line and use .user.ini instead.
  • memory_limit = -1: unlimited; avoid on production, a bad script can then take the whole server down.

PHP memory FAQ

What memory_limit should I use?

128M suits simple sites, 256M most WordPress sites, 512M large WooCommerce or Magento stores.

Does a higher limit slow the site?

No. It is a ceiling, not a reservation. But each PHP worker may use up to that much, which matters for total server RAM.

Is this the same as the server running out of RAM?

No. This error is PHP's own per-script limit. Server-wide out-of-memory errors kill processes instead.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.