Software

PHP "Allowed Memory Size Exhausted": Is Raising memory_limit Enough?

Talha Aslan 18 min read

Short answer

What does the allowed memory size exhausted error mean?

Allowed memory size exhausted is a fatal PHP error that reports the memory cap for a single request has run out. The moment a script asks for more memory than the memory_limit ceiling, PHP halts execution. As a result, visitors usually see a blank page, a half-rendered page or a generic error message.

Keep reading for the full details and examples

Short answer: "Allowed memory size of ... bytes exhausted" means a PHP script tried to use more memory than memory_limit allows, so PHP stopped it. Raising memory_limit often brings the site back right away, but that is first aid. The lasting fix is finding what eats the memory: an endless loop, a huge query, a heavy plugin or image processing.

In this guide we first show how to read the error, then the safe ways to raise the limit. After that, we give you a diagnosis table and a step by step method to find the real cause. That way you avoid the cycle of bumping the limit a little higher every month.

The allowed memory size message contains two numbers. First comes the total allowed memory, in bytes. After that comes the extra memory the script tried to allocate at the last moment. For example, 134217728 bytes equals exactly 128 megabytes. According to the PHP docs, the default memory_limit is also 128M; so if you see that number, your server most likely runs the default setting.

You can confirm this in the PHP core configuration docs. The same page notes that a value of -1 removes the memory limit entirely and that the setting is changeable at every level. However, removing the limit does not solve the problem; it lets the problem spread to the whole server.

How do you read the numbers in the error message?

The full message usually includes the allowed memory, the amount the script tried to allocate, a file path and a line number. Those four details, in practice, are half the diagnosis. The file path and line, in particular, show which plugin or piece of code blew up.

  • Allowed memory: shows the memory_limit value on your server.
  • Attempted allocation: a small number means memory filled up gradually; a large one means a huge chunk of data loaded at once.
  • File path: tells you which plugin, theme or library raised the error.
  • Line number: shows the last point where memory ran out, but that line is not always the real culprit.

That last point also matters. Memory works like a bucket; the line that adds the final drop may not be the one that filled it. For example, a plugin loads thousands of records into memory, then an innocent helper function tries to process a small string, and the error shows up on that line. So treat the file path as a clue, not a verdict.

Is raising memory_limit enough, or is the real problem elsewhere?

Short answer: sometimes it is enough, but often it is not. If your site needs more memory because of a new feature, a bigger product catalog or a legitimately heavy task, raising the limit by a sensible amount is the right fix. However, if memory use grows without control, any limit you set will fill up again one day.

To tell them apart, then, ask one question: after you raise the limit, does memory use settle at a stable level, or does it creep toward the ceiling again over time? If it stays stable, the need is real. If it keeps climbing, you have a leak, a loop or a growing data set.

Also, raising the limit has a cost. When each PHP process can use more memory, fewer processes can run at the same time. On shared hosting, that means your account hits its resource caps faster. We explain how those caps work in our guide to shared hosting resource limits. In short, raising the limit is a painkiller, not a cure; it only postpones the diagnosis.

Diagnosis table for the real causes of allowed memory size errors

The table below shows the causes we see most often in practice, their typical symptoms and how you test each one.

CauseTypical symptomHow you test itLasting fix
Endless loop or recursionThe error persists no matter the limit.Raise the limit and retry; if the error repeats on the same line, suspect a loop.Fix the loop's exit condition.
Large database queryErrors on report, export or listing pages.Run the same task with fewer records.Pagination and batch processing.
Heavy or buggy pluginThe error starts after an update.Disable the plugin and test the site.Update or replace the plugin.
Image processingErrors while uploading large photos.Shrink the same image and upload again.Resize before upload.
Genuine capacity needMemory is stable and close to the ceiling.Measure peak usage.Raise the limit in a measured way.

The last row is the only "legitimate" case for raising the limit. In the other four, a higher limit only buys time. Therefore we suggest reading the table top to bottom and ruling out each row with a test.

How do you raise memory_limit to get the site back up now?

If your site is down right now, getting it back up first makes sense. There are several ways; however, which one works depends on your hosting setup. The general rule: the method closest to the server and most official comes first.

  1. Change memory_limit in the PHP settings screen of your control panel.
  2. Without a panel, use the PHP config file your host supports.
  3. On some Apache servers you can set a PHP value through .htaccess; however, not every setup supports this.
  4. At the app level, for example in WordPress, use its own memory constant.
  5. After the change, reload the page and confirm the value is actually active.

On cPanel plans, this setting usually sits in the MultiPHP INI Editor or your host's PHP selector. Remember that switching PHP versions can reset some values; we explain that screen in our guide to changing the PHP version in cPanel.

One warning, though: if you enter a value above your plan's cap, it may look saved but have no effect. In that case, the real ceiling sits with your host.

How do WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT work in WordPress?

WordPress tries to manage the PHP memory limit with its own constants. According to the official WordPress docs, WP_MEMORY_LIMIT lets you specify the maximum memory PHP can consume. By default, WordPress attempts to raise memory to 40MB for single sites and 64MB for multisite installs.

There is also a separate constant for the admin area, specifically for the dashboard. WP_MAX_MEMORY_LIMIT lets you define a different memory value inside the dashboard. According to the docs, these definitions must come before the wp-settings.php inclusion. You can read the details in the WordPress wp-config documentation.

A common mistake lives here: the WordPress constant cannot exceed the server level PHP limit. If the server keeps memory_limit at 128M, writing a higher value into WordPress does nothing on its own. The docs also note that some hosts do not allow this automatic increase. So think about the server limit first, then the WordPress constant.

In WordPress this error often appears together with the "there has been a critical error" screen. We cover general diagnosis for that screen in our guide to the WordPress critical error.

How do endless loops and recursion eat memory?

Put simply, an endless loop is a repeating structure whose exit condition never comes true. If each pass adds a new value to memory, memory hits the ceiling quickly. Recursion, a function calling itself, works the same way; with a broken stop condition, each call opens a new memory frame.

Above all, the clearest sign of this cause: you double memory_limit, and the error comes back at the same spot, just a bit later. With a genuine capacity need, the error disappears; with a loop, it only gets postponed. That difference is one of the strongest diagnostic tests you have.

On WordPress sites, loops usually come from two plugins triggering each other. For example, one plugin starts a task when a post saves, that task saves the post again, and the chain repeats. Misconfigured redirect rules or shortcodes that call themselves produce the same picture.

  • Does the error still appear in the same file after you raise the limit?
  • Can you tie it to a specific record, page or action?
  • Does a recently added plugin or snippet hook into the same event as another plugin?

Why do large database queries cause memory errors?

When a query pulls thousands or hundreds of thousands of records at once into a PHP array, all that data sits in memory at the same time. As the record count grows, the memory need grows with it. An export screen that worked fine yesterday starts throwing this error one day as data piles up.

Specifically, these errors usually show up on reports, exports, bulk edit screens and "show all" pages. The diagnosis, however, is simple: run the same task with fewer records. For example, narrow the date range or add a filter. If the error disappears, the problem is the data volume.

The lasting fix is processing data in batches. Pulling records page by page, processing each batch and releasing it, and turning big exports into background jobs keeps memory flat. Selecting only the columns you need also cuts memory use noticeably. Problems with the database connection itself are a separate topic; we cover them in our guide to the error establishing a database connection.

How do plugins and themes inflate memory use?

In practice, every plugin loads its own code and data into memory on each page load. A few light plugins, for example, cause no trouble. However, plugins that load large settings arrays on every page, run unnecessary queries or bundle heavy libraries push the total memory need up fast.

The most reliable way to tell whether a plugin is the culprit is to disable plugins one by one. We recommend doing this on a staging copy, not the live site, whenever possible. If you cannot reach the dashboard, temporarily renaming the plugin's folder also disables it.

  1. If the file path in the error names a plugin, disable that plugin first.
  2. When the path names no plugin, disable plugins in groups and look for the error.
  3. Once you find the culprit, check its latest version and read the changelog.
  4. If the latest version still misbehaves, consider a lighter alternative that does the same job.

Themes also work the same way. Themes packed with ready sections and features may load unused code on every page. We compare this tradeoff between ready themes and custom code in our article on WordPress vs a custom website.

Why does image processing need so much memory?

To resize or crop an image, PHP first turns it into an uncompressed bitmap. Even if the file on disk is small, its size in memory scales with the pixel count. That is why high resolution photos use far more memory than their file size suggests.

For example, as a sample calculation: a 6000 x 4000 pixel photo has 24 million pixels. At four bytes per pixel, the uncompressed image holds roughly 96 megabytes in memory. The image library may also create intermediate copies. So a file of a few megabytes on disk can push a 128M limit on its own. This is an estimation model; real usage depends on the library and the operation.

Systems like WordPress generate several smaller sizes for every uploaded image, which adds more load. The most effective fix is resizing images to a sensible width before upload. We cover upload size limits in detail in our sibling guide on 413 Request Entity Too Large.

How do you measure memory use?

So measuring instead of guessing cuts diagnosis from hours to minutes. PHP offers built-in functions for this. The memory_get_peak_usage function returns the highest amount of memory, in bytes, allocated to the script so far. The details sit in the PHP memory_get_peak_usage documentation.

According to the docs, the function takes one parameter. By default, it reports only the memory used by PHP's own allocator. When you pass true, it returns the real size of memory allocated from the system. During diagnosis, checking both values helps.

  • Log the peak memory value at the start and end of the problem task.
  • Repeat the same task with different data volumes and compare the values.
  • If the value grows with data volume, move to batch processing.
  • If the value climbs without limit on every run, look for a loop or a leak.

Use measurement code only on staging or temporarily, and remove it when you finish. Showing measurement output to visitors on a live site exposes server details.

What should you look for in the error logs?

The message on screen is often incomplete or hidden. The real information lives in the error log. There, the PHP error log records the full allowed memory size error text, its time, file path and line number. In most hosting panels you find it under the error logs section.

When reading the log, then, look at three things. First, how often the error repeats. Second, whether it always appears in the same file or in different files. Third, whether it clusters at certain hours. Errors that cluster at specific times usually point to scheduled tasks or bot traffic.

To understand bot impact, check the access logs as well. We explain the basics in our server log file analysis guide. Our log file analyzer also helps you spot heavy request patterns in access records quickly. For instance, a bot calling the same heavy page dozens of times a minute can trigger memory errors in bulk during those hours.

How do scheduled tasks trigger memory errors?

Some allowed memory size errors do not appear on visitor pages but in background tasks. A nightly backup, a bulk email send, a stock sync or sitemap generation, for example, all handle large data. So if your error log shows memory errors repeating at certain hours, check the list of scheduled tasks first.

On WordPress sites, scheduled tasks often fire on a visitor request. As a result, a heavy task can surface as a memory error on a random visitor's page. The visitor opens an innocent page, but the task that starts in the background eats the memory.

  • Compare the error times with the run times of scheduled tasks.
  • Move heavy tasks off visitor requests and onto the server's own scheduler.
  • Split bulk operations into small batches and release memory between them.

Also make sure heavy tasks do not overlap. If a backup and a stock sync start in the same minute, they may exceed the limit together even though each runs fine alone. In short, the task schedule is part of memory planning too.

When is upgrading your hosting plan the right call?

Upgrading makes sense, however, only when there is a genuine capacity need. For example, your site grew, your product count went up, and measurements show peak memory steadily close to the ceiling. In that case, a plan with more memory and more concurrent processes is a sound investment.

However, if measurements reveal a loop or an uncontrolled query, upgrading means spending money in the wrong place. A bigger plan only produces the same error a little later. That is why you should rule out the first four causes in the diagnosis table before deciding.

Moreover, a plan change can also change the PHP version, plugin compatibility and cache settings. So after upgrading, repeat the same measurements and compare peak memory with earlier results. Then you see in numbers whether the investment actually worked.

What is the right step-by-step diagnosis order?

The sequence below lets you first get the site back up and then find the real cause. Change only one variable per step, so you know exactly what worked.

  1. Copy the full error text, file path and time from the log.
  2. If the site is down, raise memory_limit temporarily to a sensible level.
  3. Watch whether the error disappears or repeats at the same spot.
  4. If the file path names a plugin or theme, disable it on staging.
  5. If the error appears during a specific task, repeat it with less data.
  6. For errors during an image upload, shrink the same image and upload it.
  7. Measure peak memory use and note the results.
  8. After fixing the real cause, consider bringing the limit back to a sensible level.

Still, people often forget the last step. If a temporary limit becomes permanent, the next problem shows up later but with a bigger impact. So log the limit change like any other change. We also suggest keeping a small comparison table: for each attempt, write down what you changed, which data you tested with and the peak memory result. After a few attempts, the pattern shows itself.

A realistic example scenario: the order report crashes a little later each month

Here is an example scenario. An online store manager gets an allowed memory size error while exporting the monthly order report. Hosting support raises memory_limit and the report works. Two months later the same error returns, and support raises the limit once more.

In other words, this pattern points to a growing data set rather than a genuine capacity need. The report loads more orders into memory at once every month. Therefore raising the limit only pushes the problem to next month. On top of that, each increase lowers the number of PHP processes that can run at the same time.

The lasting fix is moving the report to a structure that processes data in batches. Reading orders page by page and writing them to a file makes memory use independent of data volume. Running the report as a background job also frees the manager from waiting on the browser. Then the limit can stay flat no matter how many orders come in. The lesson is simple: if the same error returns at regular intervals, the problem is design, not capacity.

What are the most common mistakes?

These are the mistakes we see most often with this error:

  • Setting memory_limit to -1 and removing the limit completely.
  • Raising the limit a little more each time the error returns and never looking for the cause.
  • Raising the WordPress constant without noticing the server limit is lower.
  • Treating the line in the error message as the only culprit and working only on that file.
  • Disabling plugins at random on the live site and showing visitors broken pages.
  • Forgetting measurement code on the live site.

Above all, the first point is dangerous. With no limit, a loop or a huge query can drain all server memory. Then not only your page but other processes on the same server suffer too. We cover the server wide effects of memory exhaustion in our guide on diagnosing a slow server.

How do you tell an allowed memory size error from a JavaScript heap error?

Developers sometimes confuse the PHP memory error with the JavaScript heap error from a Node.js build. Both describe memory running out, but they happen in completely different environments. The PHP error appears on the server, in a script that runs on every visitor request. The Node.js error usually appears during a build or dev step, in the terminal.

So the fixes also differ. For PHP, memory_limit and code level improvements matter; for Node.js, the process memory setting and build configuration come into play. For the Node.js case, see our guide to JavaScript heap out of memory.

In short, look at where the error appears: in a visitor page or the server log means PHP, while the terminal after a build command most likely means Node.js.

Do caching and PHP settings affect memory use?

Yes, indirectly. Compiled code caches such as OPcache keep PHP from recompiling files on every request and improve overall performance. However, OPcache's own memory pool is not the same thing as memory_limit. Mixing up these two concepts leads you to tune the wrong setting.

A page cache can also reduce allowed memory size errors indirectly. When most visitors receive a ready page from cache, heavy PHP work runs less often. But that is masking, not fixing; the moment the cache clears, the same error returns. We explain OPcache settings in our OPcache guide. For how account isolation relates to memory caps on shared hosting, see our CageFS guide.

When should you get expert help?

If raising the limit brings the site back but the allowed memory size error returns a few weeks later, the problem is not a setting but code or architecture. Custom dashboards, reporting screens and integrations fall into this group in particular. At that point, someone needs to read the code and redesign the data flow.

As Talha Aslan and team, we take over existing software, map its risks through a technical audit and, where needed, rebuild it module by module. Report and integration structures whose memory use grows out of control are among the topics we handle often in our custom software development work.

What does our custom software development work cover for problems like this?

In short, our work covers discovery and technical design, web apps and portals, SaaS products, APIs and integrations, security and testing, and modernization and maintenance. We run development against an OWASP Top 10 and ASVS based checklist and write automated tests. Source code, repository and cloud accounts are in your name from day one.

For recurring problems like memory errors, we first map the business rules and the data flow, then rewrite the problem module against measurable goals. After go-live, we handle updates, monitoring, backups and bug fixes under a written maintenance plan. You can find the scope and process on our software development services page.

Frequently Asked Questions

Is setting memory_limit to -1 safe?
No, usually it is not. According to the PHP docs, a value of -1 removes the memory limit completely. A loop or a very large query can then drain all server memory and affect other processes on the same server. Instead of removing the limit, choose a measured value and look for the real cause.
I raised WP_MEMORY_LIMIT but the error persists. Why?
The WordPress constant cannot exceed the server level PHP memory limit. If the memory_limit value on the server is lower, the value you write into WordPress has no effect. The official docs also note that some hosts do not allow this automatic increase. Check the server value first, then set the WordPress constant to match.
Is the file in the error message always the culprit?
No, not always. The file and line in the message show the last point where memory ran out. The task that actually filled memory may have run earlier in another plugin or query. Use the file path as a strong clue, but also test by disabling the plugin and changing the data volume.
What is the ideal memory_limit value?
There is no single ideal value that fits everyone. The right value depends on your site's real peak memory use and your hosting plan's resources. First measure the peak usage of the problem task, then choose a value a sensible margin above it. Also check the cap your plan allows with your hosting provider.
I get a memory error when uploading images. What should I do?
The most effective fix is resizing the image to a sensible width before upload. PHP turns an image into an uncompressed bitmap while processing it, so memory needs grow with the pixel count. A smaller image prevents the memory error and protects page speed. If needed, you can also raise the limit in a measured way.
Does this error mean my site was hacked?
Not on its own. Most memory errors come from plugins, queries or image processing. However, if the error clusters at certain hours and the access logs show heavy requests you do not recognize, consider bot traffic or malicious requests as a possibility. In that case, reviewing the logs together with an expert is worthwhile.
  • allowed memory size
  • memory_limit
  • php memory error
  • wordpress memory
  • WP_MEMORY_LIMIT
  • php debugging
  • fatal error
Share:
Talha Aslan

Google Partner digital marketing expert. Hands-on with SEO, Google Ads, web design and e-commerce projects since 2012; every post here comes from that experience.

Next project

Get a quote for your project.

Your brief goes straight to Talha Aslan and team: strategy led by Talha, delivery by an experienced team. We reply the same day and send the scope, timeline and price as a written quote within 1 business day.