What is PHP Memory Limit?

Blue screen on monitor

The PHP memory limit is a critical configuration setting in web development that dictates the maximum amount of memory a PHP script can use. Understanding and managing this limit is essential for ensuring the smooth operation of websites and web applications. Let’s explore what PHP memory limit is, why it matters, and how to configure it for optimal performance.

Key facts infographic: PHP Memory Limit
Key facts at a glance

Understanding PHP Memory Limit

The PHP memory limit specifies the maximum amount of server memory a PHP script can utilize while executing. This limit prevents poorly written or resource-intensive scripts from consuming excessive server resources, which could lead to website crashes or slow performance.

Default PHP Memory Limit

Most web hosting providers set a default PHP memory limit, which typically ranges between 32MB and 128MB. While this default setting is sufficient for basic websites, more complex applications may require a higher limit to handle advanced features or plugins.

Why PHP Memory Limit Exists

The PHP memory limit acts as a safeguard, protecting your server from being overwhelmed by scripts that demand excessive resources. Without this limit, a single poorly optimized script could slow down or crash your entire website.

Why is PHP Memory Limit Important?

Setting an appropriate PHP memory limit ensures your website performs efficiently while minimizing the risk of server errors. Insufficient memory can result in scripts failing to execute, causing issues like:

  • White Screen of Death (WSOD): A blank page indicating PHP ran out of memory.
  • 500 Internal Server Errors: Caused by exceeding the memory allocation.
  • Plugin or Theme Failures: Resource-heavy plugins or themes may not function properly.

Impact on Website Performance

The PHP memory limit directly affects your site’s ability to handle resource-intensive processes, such as:

  • Processing large datasets
  • Running advanced themes or plugins
  • Managing high-traffic spikes

Setting the right limit ensures these processes run smoothly without compromising overall performance.

How to Adjust PHP Memory Limit

If your website encounters memory-related issues, you can increase the PHP memory limit. Below are the common methods to do so.

Adjusting via php.ini

  1. Locate the php.ini file on your server.
  2. Search for the line memory_limit.
  3. Increase the value (e.g., memory_limit = 256M).
  4. Save the file and restart the server for the changes to take effect.

Editing .htaccess

  1. Access the .htaccess file in your website’s root directory.
  2. Add the following line:Copy codephp_value memory_limit 256M
  3. Save and upload the file.

Using wp-config.php (for WordPress)

  1. Open the wp-config.php file in your WordPress installation.
  2. Add the following line before the “That’s all, stop editing!” comment:phpCopy codedefine('WP_MEMORY_LIMIT', '256M');

Best Practices for Setting PHP Memory Limit

While increasing the memory limit can resolve issues, it’s important to find a balance. Setting the limit too high may allow inefficient scripts to run unchecked, while a limit that’s too low could hinder functionality. Always monitor your website’s memory usage and optimize scripts to ensure efficient resource utilization.

How to Check Your Current PHP Memory Limit

Before you change anything, find out what your limit is now. In WordPress, open Tools, then Site Health, then the Info tab, and expand the Server section. You can also create a small PHP file and run it.

<?php
echo ini_get('memory_limit');

Delete the file after you test it. If you prefer, phpinfo() shows the same value together with many other settings.

Common PHP Memory Limit Errors

When a script needs more memory than the limit allows, PHP stops it and shows a message like the one below. Visitors may see this message, or they may only see a blank page.

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

The number is the limit in bytes, which is 128 MB in this example. On WordPress sites, the usual causes are heavy plugins, page builders, large image processing, or imports. A blank page after this error may look like the problem described in our guide on how to fix the White Screen of Death on WordPress.

Recommended PHP Memory Limit Values

Type of siteTypical memory limit
Small blog or brochure site128M
Business site with a page builder or several plugins256M
WooCommerce store or large membership site256M to 512M

These are general starting points. Your hosting plan may set a maximum, and some providers do not allow you to exceed it.

Frequently Asked Questions About the PHP Memory Limit

Is a higher PHP memory limit always better?

No. Raising the limit can hide a problem such as a poorly written plugin. If you must keep increasing it, find the script that uses the memory instead.

What if my host will not allow me to increase it?

Contact your hosting provider, deactivate heavy plugins, or move to a plan with more resources.

Reduce PHP Memory Use Without Raising the Limit

  • Deactivate and delete plugins that you no longer use.
  • Update to a supported PHP version, since newer versions are usually faster and more efficient.
  • Compress and resize large images before you upload them.
  • Use a caching plugin so that pages do not need to be rebuilt for every visit.
  • Limit post revisions and clean old data from the database.
  • Run large imports or backups in smaller batches.

These steps often solve memory errors more safely than a higher limit, and they also make your site faster.

How can I find out which plugin uses the most memory? Deactivate plugins one at a time on a staging site and repeat the action that caused the error. You can also use a debugging or profiling plugin, or ask your host to check the server error log, which usually shows the file that ran out of memory.

By understanding and managing the PHP memory limit, you can improve your website’s performance and ensure a smooth user experience. For web developers and site owners, this configuration is a vital part of maintaining a reliable and scalable web presence.