Skip to content
hostcomplex

What Is the PHP Memory Limit?

Maarten Soetens 3 min read

What the PHP Memory Limit Controls

The PHP memory limit is the maximum amount of memory a single PHP script may use while it runs. It is set by the memory_limit directive in the php.ini file, and it applies to each script run separately.

Memory here means the RAM used by the script itself, including the variables it creates, the data it loads from a database and the arrays it builds while handling a request. When a script goes past the limit, PHP stops it and logs a fatal error, typically reading Allowed memory size of ... bytes exhausted.

The limit protects the rest of the server. Without it, one badly written script could consume all available RAM and slow down or crash every other site on the same machine.

Why Scripts Hit the Limit and What Affects It

Most memory errors come from work that grows with the data. A script that loads thousands of rows into an array at once, resizes large images in memory or builds a long report can exceed a limit that is fine for small pages. Plugins and frameworks add their own overhead, so a site can start hitting the limit as it grows, even when its code has not changed.

Two factors set the practical ceiling. The first is the configured value of memory_limit. The second is the RAM the hosting plan actually provides, because a higher limit does not create more memory. Hosts usually choose a value that fits the plan, since a high limit on a small plan lets one site take a larger share of shared resources.

Raising the limit can be the right fix, but it treats the symptom. If a script needs far more memory than it should, processing data in smaller batches or in a background job is usually the more durable solution.

Example: A Report Export on a Growing Store

An online shop generates a yearly sales export with a single PHP script. For its first few years the export works. As the order history grows, the script loads every order into memory before writing the file, and one day it stops with a memory error. The team first suspects the server, but the log shows the script simply used more memory than its limit allows.

The lasting fix is to change how the script handles data, reading and writing orders in chunks. This keeps memory use roughly flat as the store grows, which a higher limit alone cannot guarantee.

Frequently asked questions

Is it safe to set memory_limit to unlimited?

It is generally not recommended. Setting the value to -1 removes the cap, so a runaway script can consume all available RAM and affect other sites or services on the same server. A moderate limit that matches the plan, combined with efficient code, is the safer choice.

Does the PHP memory limit affect file uploads?

Not directly. Upload size is governed by settings such as upload_max_filesize and post_max_size. However, processing an uploaded file, for example resizing an image, can use a lot of memory, so the memory limit may stop the script after the upload has already succeeded. Learn how these failures appear in PHP errors.

  • Two-Factor Authentication

    Two factor authentication hosting adds a second check to control panel, domain and CMS logins. Learn which methods are strongest and why 2FA helps.

  • What Is an A Record?

    What is an A record? Learn how this DNS record maps a hostname to an IPv4 address, why domains use several A records and the root and www pattern.

  • Billing and Automation Platforms

    What is WHMCS? Learn how this commercial billing and automation platform helps hosts sell plans, collect payments and create accounts automatically.