What is the PHP Memory Limit and How Change it?

To change upload size in DirectAdmin you need to edit php.ini. Here we will show with simple steps how to make this happen.

Updated: 17 Oct, 24 by linda Y 11 Min

List of content you will read in this article:

Scripts are some of the most convenient pieces of code that one could write. The lack of need for compilation and the fact that they are much faster to develop than conventional programs makes them very appealing to use. They do, however, have a big downside. When talking in the context of server-side scripting, a poorly written script can easily eat up all of the available server resources and might even cause other parts to malfunction. Thus, in PHP, a script programming language, there is a default cap to how much of the server’s memory a single script can use. While being much needed in most applications, certain cases require this limit to be increased. Today we will be showing you how to do exactly that, as well as give you a few other useful tips on increasing different limits within the PHP configuration. First, however, let us dive into a short explanation about PHP memory limits.

When you visit a WordPress site, PHP is behind the scenes. PHP is a server-side programming language that makes it possible to create and manage dynamic HTML pages. Why do we say it is behind the scenes? Actually, because it runs on the server instead of your personal PC or mobile. Using PHP, you can make website components. There are many reasons that expert developers use it: it is fast, open-source, and lightweight. Also, it is possible to change and modify the best PHP frameworks for specific needs and this is a big feature for developers. Also, for better performance and security, PHP frequently is updated. There are some PHP editors that make it easy to write clean PHP codes. 

If we want to mention the connection between WordPress and PHP in short, we can say PHP is the backbone of WordPress which makes it flexible and powerful to make a website. 

32MB! Yes, that’s the memory limit that WordPress sets a memory limit. 32MB usually is enough for smaller websites which have a lower number of pages, and minimal content. But, when the time passes and you add more content to your website, including pages, images, plugins, and videos, you may pass that limit. In this case, the limit will be increased to 40MB. So, what happens when you see the error and cannot add more content? it happens when you reach the 40MB limit. As a result, it prevents you from adding more things to your website. But there are other reasons for PHP memory limit which we talk about in the next section. 

The good news is you can increase PHP memory limit in WordPress. But before you do that, it’s better to find the reason for memory overuse. If not, you may still encounter this issue because when the cause of the error is not for available space, for example, it is because of something like a specific element in your site, this problem won’t be solved. In this case, adding more memory won’t solve the problem. So, let’s see what can cause memory limit PHP:

  • Poor-Quality Plugins: There are some inefficient plugins that use a large amount of RAM. Using many memory-hungry plugins can overload PHP memory and cause issues.
  • Outdated PHP Versions: When you use old PHP versions, performance concerns can happen. As updated versions handle tasks more effectively, less memory will be used. So, you should update PHP in WordPress
  • Memory-Hungry Media: Some content, for example, videos or interactive elements, can use so much more memory. On the other hand, sites that have minimal media and are text-based use less memory. 

Should you maximize your memory bandwidth?

If you're unsure why your website has memory issues, try utilizing a plugin called Server IP and Memory Usage Display. This plugin monitors how much memory your WordPress site is using and alerts you if you are running out. It also displays you which plugins are utilizing the most RAM, allowing you to identify the source of the problem.
So, now, should you increase PHP memory limit or stay under that for better performance? Actually, it’s better to use lower memory to stay under the limit. If not, your site’s performance may decrease. But if you want to know how to increase the memory limit PHP, continue reading. 

How much PHP memory is recommended for WordPress?

You can change the PHP memory limit in WordPress to 64MB and even 256MB. But note that increasing the memory and using data-heavy elements results in slower site speeds. So, we recommend staying in balance in the middle to optimize both memory usage and performance. 

Unlike what many might believe, the memory_limit setting in PHP is on a per-script basis, meaning it is the maximum amount of server memory a single PHP script is allowed to consume. The official PHP documentation puts it this way:

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.

Source: https://www.php.net/manual/en/ini.core.php#ini.memory-limit

Let’s further break it down with an example. If you have not changed the default memory limit setting in PHP, then it will be set to 128M. Let’s say you are running seven scripts, each one requiring 80M. Despite your overall memory usage being 560M, it still does not hit the 128M limit as each of the scripts uses less than that. Now if one of the scripts suddenly decides it needs 130M, that’s where an issue will occur. An error like this will appear:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script

or:

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script

Let us move on onto how to get rid of these errors.

Follow these simple steps in order to increase your PHP memory limit in DirectAdmin:

Step 1: To change the PHP memory limit in DirectAdmin, you need to edit php.ini file but first, you need to find it. A new and easy trick to find your php.ini would be to ask PHP itself what it's using, e.g.:

/usr/local/bin/php --ini | grep 'Loaded Configuration File'

If you're running the default version of CLI PHP, your php.ini will be located at:

/usr/local/lib/php.ini

Step 2: Now you will need to edit the ”memory_limit” line to your desired values. If no "memory_limit" found, add the following line at the end of php.ini

memory_limit = 128M; /* Change the 128M to your needs */Save file.

Step 3: Now, restart Apache.

Additional Notes: Also consider that the following settings may also be related:

If you want to increase the maximum post data size:

post_max_size = 8M

If you wish to change the maximum allowed size for uploaded files:

upload_max_filesize = 2M

Lastly, some RoundCube versions have limits set in the following file:

/var/www/html/roundcube/.htaccess

Thus, be sure to increase them there as well if they're set.

If you have done these:

  • Updated your PHP
  • Removed low-quality plugins
  • Cleaned up your content

But you still see the memory errors, which means you need to increase the memory limit PHP in WordPress. Here we tell you how to do with different methods:

Method 1: Modify Your wp-config.php File

If you don’t need anything but basic technical tasks, you can increase your memory by changing wp-config.php file. To do that, you need to use an FTP or SFTP client to connect to your WordPress files. After connecting, locate the wp-config.php file like the following picture:

and search for the following line: 

define('WP_MEMORY_LIMIT', '32M');

Then change it to:

define('WP_MEMORY_LIMIT', '128M');

if you really need more memory, you can boost it to 256MB. But it is not recommended because you won’t need that much memory. 

Method 2: Adjust Your PHP.ini File

Are you still encountering the problem? So, if the last method didn’t solve the issue, try this method that includes changing your PHP.ini file. Attention, if you're on shared hosting you may not have access to this file. First of all, search for this line:

memory_limit = 32M

and change it to:

memory_limit = 128M

You may also desire to adjust the max_execution_time parameter, which regulates how long a PHP script can execute before it stops operating. If you're utilizing plugins or services that require a lot of memory, increase this setting to allow them more time to finish.

Method 3: Edit Your .htaccess File

You can change the memory limit in WordPress by changing the .htaccess file too. The dot at the beginning of its name shows that this file is hidden. So, if you want to see it you need to set the FTP or SFTP client to “show hidden files”. After locating the file, find this line:

php_value memory_limit

Then, add your desired number of memory limit (64M, 128M, or 256M) and save the changes. 

Note: Always remember to clear your cache after making the changes. In this case, you can see if the problem is solved. 

Method 4: Use a Memory Increase Plugin

You can also use a plugin to boost the PHP memory limit. WP Memory Limit, Memory Usage, and Server Memory and Health are all compatible with WordPress versions 5.2 and up. These plugins allow you to adjust the amount of RAM your website can utilize without having to update any code.

Method 5: Reach Out to Your Hosting Provider

In the end, you can contact your hosting provider and ask them to increase your WordPress PHP memory limit. The ability to customize your PHP memory limit depends on the type of hosting service you use. It may be simple to implement if you have a shared hosting package. However, your hosting provider may not offer this option if you have a dedicated or virtual private server.

The PHP memory limit exists for a good reason. Without it, a poorly written script can easily eat up all of your server resources and cause many issues. If you have a beefier system, however, and wish to utilize more of your resources towards PHP scripts, you can change the maximum allocated memory for each script by simply editing a configuration file. Today short tutorial showed you how to do exactly that. If you have any questions or suggestions, please leave them in the comment section below.

linda Y

linda Y

My name is Linda, I have Master degree in Information Technology Engineering. I have some experiences in working with Windows and Linux VPS and I have been working for 2 years on Virtualization and Hosting.