Skip to content
PHP and Laravel3 min read

"Allowed memory size of ... bytes exhausted": causes and fixes

Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

Short answer

The error means a PHP script used all the memory memory_limit allows, so PHP stopped it. The first number is the limit in bytes (134217728 = 128 MB). Raise the limit in Select PHP Version or MultiPHP INI Editor if the task is genuinely heavy; if the error returns at the higher limit, the code is loading too much data at once.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. What the error means
  2. Common causes
  3. How to fix it
  4. Prevention
  5. On ArabyHost

You’ll see this during a large import, a report that reads a whole table, image processing, or in WordPress with heavy plugins. Every PHP script has a memory cap set by memory_limit, and that cap applies to one process, not to the whole account.

What the error means

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /home/user/app/Export.php on line 58
  • 134217728 bytes: the current limit in bytes.
  • tried to allocate 20480 bytes: the last chunk the script asked for and couldn’t get.
  • Path and line: where it stopped, usually inside the loop or function collecting the data.
Bytes Megabytes
67108864 64M
134217728 128M
268435456 256M
536870912 512M

Common causes

  • Loading every row of a big table at once, such as SELECT * or Model::all() in Laravel.
  • Reading a big file whole with file_get_contents() instead of line by line.
  • Processing high-resolution images.
  • An endless loop or runaway recursion that keeps adding data.
  • Heavy WordPress plugins; see memory exhausted in WordPress.
  • Composer while installing packages, which has its own page: Composer memory limit.
  • A low memory_limit such as 64M.

How to fix it

  1. Locate the code from the path and line in the message; the full text is in cPanel under Metrics › Errors.
  2. Raise the limit in the panel: open Software › Select PHP Version, go to Options and set memory_limit to 256M or 512M. Or in Software › MultiPHP INI Editor, pick the domain and change the value. More on php.ini.
  3. Other ways to set it:
# in .htaccess
php_value memory_limit 256M
// at the top of one specific script
ini_set('memory_limit', '256M');
# for a single command-line run
php -d memory_limit=512M artisan import:products
  1. If the error comes back at the new number, fix the code: process data in batches instead of loading it all. In Laravel:
// instead of User::all(), which loads every row
User::chunk(500, function ($users) {
    foreach ($users as $user) {
        // process each user
    }
});

// or one record at a time
foreach (User::lazy() as $user) {
    // ...
}

For a big CSV, read it line by line:

$fh = fopen('products.csv', 'r');
while (($row = fgetcsv($fh)) !== false) {
    // handle the row
}
fclose($fh);
  1. Measure usage to see where it climbs:
echo round(memory_get_peak_usage(true) / 1048576) . " MB\n";

Prevention

  • Avoid all() and SELECT * on tables that grow over time; use pagination and batches.
  • Move heavy jobs such as exports and imports to cron or queues, not page requests.
  • Check memory use after shipping any feature that handles a lot of data.

On ArabyHost

You can set memory_limit per site in cPanel and choose PHP 8.1, 8.2 or 8.3. Keep in mind that memory_limit caps a single process, while each account also has a total memory limit under LVE. If your processes together hit the account limit, you’ll see an error such as 508 Resource Limit Reached instead of this message, so raising memory_limit doesn’t add account memory. On AI/Developer plans you get SSH to run heavy jobs from the command line, and the Araby AI assistant reads the error log and points to the code responsible.

FAQ

How many MB is 134217728 bytes?

128 MB, since a megabyte here is 1048576 bytes. 268435456 bytes is 256 MB and 536870912 bytes is 512 MB.

Should I set memory_limit to -1?

Not on a website. -1 removes the limit, so a broken script can eat all of the account's memory and slow every site on it. Use a fixed value such as 256M or 512M.

Why does "tried to allocate" show such a small number?

It's only the last request that didn't fit, not the cause. Memory filled up gradually before it, so look for the code that accumulates a lot of data in memory.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.