🧠
Hosting

"Allowed memory size exhausted": fixing the PHP memory error properly

09.11.2026
← All articles

A blank page plus a log line such as Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) means a PHP script hit its memory ceiling. The first number is the limit in bytes (134217728 bytes is exactly 128 MB); the second is just the last small allocation and says little about the root cause. You can raise the limit or find the code that wastes memory, and often you need both.

What memory_limit does

memory_limit caps the RAM a single PHP request can use. It is a safety net: a buggy loop or infinite recursion gets stopped before it eats the whole server. Setting it to -1 (unlimited) is a bad idea, especially on shared hosting. 128 MB is fine for typical sites, 256 MB is common for heavy CMSs and online stores, and large imports or image processing may temporarily need 512 MB. Check the current value with a temporary file and delete it afterwards:

<?php echo ini_get('memory_limit');

Where to raise it

The right method depends on how PHP runs on the server. Most hosts run it as a separate process (PHP-FPM or CGI), some as an Apache module.

The hosting panel

The most reliable option. ISPmanager lets you change the PHP version and key parameters, including memory_limit, per site (available options may depend on the plan). sayt.uz customers open the panel from the hosting page of the sayt.uz cabinet.

.user.ini for FPM and CGI

Create .user.ini in the site folder. PHP re-reads it on an interval (300 seconds by default), so the change applies after a few minutes:

memory_limit = 256M

.htaccess, only with mod_php

php_value memory_limit 256M in .htaccess works only when PHP runs as mod_php. Under FPM or CGI Apache does not recognise the directive and the whole site returns 500, so remove the line immediately if that happens.

In code and WordPress

ini_set('memory_limit', '512M');

// wp-config.php
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');

WP_MEMORY_LIMIT applies to regular pages and WP_MAX_MEMORY_LIMIT to the admin area and image processing. Neither can exceed the server's PHP limit.

When raising the limit is not the fix

If the error keeps coming back at 512 MB and then 1 GB, the code is at fault. Usual suspects:

error_log('Peak: ' . round(memory_get_peak_usage(true) / 1048576, 1) . ' MB');

Measure at several points to find the culprit, and process large data in batches of 500-1000 rows with LIMIT and OFFSET instead of collecting everything in one array.

Example: an export in a Tashkent online store

An appliance store in Tashkent runs example.uz on WooCommerce and exports all products to Excel weekly. Past 12,000 products the export started failing. Raising memory_limit to 256 MB in the panel helped briefly; measurements then showed the export plugin loading every product with images and metadata at once. Enabling batched export of 500 items kept usage around 90 MB with no further increase needed.

Plan-level limits

Shared hosting may also have an overall memory limit per plan. If memory_limit is fine but the site still struggles under traffic, look at the combined usage of all processes: review logs, remove unneeded plugins and enable caching. If the project has outgrown its plan, a larger plan or a VPS is the next step.

Related articles

🤖 Running a Python Bot 24/7 with systemd and PM2 🧠 Out of Memory (OOM): Why PHP and Python Processes Get Killed 🚀 Full-page caching: when it helps and when it gets in the way ⚡ Deploying FastAPI to a Server: Uvicorn, systemd and Nginx
🌐 Language
🇺🇿 O'zbek 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓