How to set php memory limit

How to Increase PHP Memory Limits

Why are PHP memory limits important to your website development journey? PHP is a famous backend technology that is used by many tech giants for supporting their applications. PHP gives many advanced features for making web pages dynamic and integrating some features you can not simply get using javascript, HTML, and CSS.

Whenever you set up a new PHP project, some memory is allocated automatically. This memory is mostly suitable for general applications. But there are some cases, for example when you are loading some heavy images when your website is using high graphics then you get some errors like —

“Fatal error: Allowed memory size of xxxxxx bytes exhausted” and

“filename.jpg exceeds the maximum upload size for this site.”

The best way to solve this error is to contact the hosting provider and increase the memory limit of the application. But there are ways to increase the memory limit of the whole website or a particular script without any expert help like using php.ini file, .htacess file, etc. In this blog, we will discuss the various strategies for increasing memory limit and the benefits of increasing the memory limit of your PHP application.

What is the PHP memory limit?

PHP memory limit is per script memory allotted to the PHP script. It is the same as the storage limit a particular task can occupy. This memory limit in PHP scripts is useful in some cases. For example, there can be cases when some poorly written code tries to eat up all the memory in the stack.

Most WordPress websites have a memory limit of 32M, but you may require more memory in many cases. For example, if you are doing heavy operations like recurring calls to the database and heavy image processing, you need to increase the memory limit of your script.

Strategies to Consider

In this part of the blog, we will share various ways to increase the memory limits of your PHP scripts/apps . While these are not the only ways to increase the memory limits of your PHP script, these are the ideal steps that most developers use for memory limit issues.

Also, changing the memory limit of an app can create problems sometimes, so you should always back up the data of your system.

Before trying to increase memory, you should always talk to the server providers of your website. They can help you increase the memory limit using their best practices.

Strategy 1: Edit the PHP.ini File

The php.ini file is executed every time a PHP application runs, and it’s used for controlling the various settings of PHP script like maximum upload size, memory limit, timeout limit, etc.

Читайте также:  Html png фон не прозрачный

To increase the memory limit, you can change the following variables. But beware, these variables are case sensitive, and you need to restart the server after doing changes for them to be reflected.

memory_limit = 256M upload_max_filesize = 12M post_max_size = 13M file_uploads = On max_execution_time = 180

The max execution time refers to the timeout of the PHP script, and it means the maximum time for which the screen can be run.

Strategy 2: Edit The HTAccess File

The .htacess file is a secret file hence its name starts with a dot. If you are using the shared hosting or, for some reason, you cannot access the php.ini file, you need to edit the .htacess file to increase the memory limit.

There are various use cases of this .htacess file. You need to add the following lines to this file to increase the memory limit.

php_value memory_limit 256M php_value upload_max_filesize 12M php_value post_max_size 13M

Strategy 3: Edit your wp-config.php File (If Working in WordPress)

Wp-config.php is one of the most critical files in WordPress sites. It is the configuration file of the WordPress sites.

For the memory limit, you can find a variable named WP_MEMORY_LIMIT in the config. Generally, the value of this is 32M. But you can increase the limit by altering this variable. For example, you can do something like this:

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

You can increase the memory to whatever limit you want and just save and close the config file to roll out the changes.

Strategy 4: Increase via CPanel

cPanel contains most of the information that is needed to run the website. If you cannot increase the memory limit using any one of the above methods, you need to increase it via the cPanel admin dashboard. So if you have access to cPanel, then to change the memory limit, follow the given steps:

  • Login to your cPanel admin dashboard. Then select the PHP version of your website.
  • Now go to options for this PHP version and find the memory limit column there.
  • There you can change easily change the memory limit of your PHP script, and your changes will be automatically saved.

Strategy 5: Increase via ini_set() Function

The ini_set() function is used to set the value of a particular attribute in the script’s context only. It is considered the safest of all the above ways because it only sets the value for the script particularly and restricts the poorly written scripts from consuming all the memory on the server.

To use this function to increase the memory limit, you can simply do.

ini_set('memory_limit', '512MB');

The above function will set the memory limit at 512 MB. Also, the ini_set() function is used only to set the value of a variable temporarily; once you close the script and restart it, it will take original values from the php.ini file.

Key Takeaways

There are many common errors like this memory limit one. In this blog, we have discussed the different strategies for increasing the memory limit in your script. The point to be noted here is that you should always raise the memory limit of your PHP script only as a last resort because this is a crucial task and can impact your site in many ways.

Читайте также:  Renaming folder in java

By the way, after making your website live to the actual customers, you must want to know what your users are doing on your website and the difficulties they are facing. It will help in increasing engagement and customer retention also. Scout APM is the best modern solution for the monitoring of your PHP application. It comes with support for many languages like PHP, Ruby, Python, etc. You will get 24×7 customer service, and pricing is very low compared to the market competitors.

We also offer a 14-day free trial without any credit card. So if you want to scale your business to the next level, start your free trial now !

Источник

4 ways to increase PHP memory limit

PHP101.Net - How to increase PHP memory limit

Increasing PHP memory limit (setting name: memory_limit ) is usually needed in many situations, especially when certain PHP platform scripts like WordPress tells you that it needs more memory for PHP to run more efficiently. Fortunately, this is very easy to do. This article will guide you on how to increase PHP memory limit in different ways.

Quick solution

  1. Ask your host to adjust it for you.
  2. In php.ini , locate and change the memory_limit configuration. Here, we will locate the setting on php.ini and change the value to:
php_value memory_limit 256M

What is PHP memory limit?

PHP memory limit is simply a configuration directive to define on the maximum amount of memory in bytes that a script is allowed to allocate, as in PHP.net. This helps to prevent poorly written PHP script to eat up the server memory, or malicious PHP scripts taking up the server bandwidth.

On the contrary, too low memory limit value can cause issues on the PHP scripts that naturally need to have fair memory allocated for it to run properly.

How to get PHP memory limit?

We have a tutorial article here on how to get the PHP memory limit with multiple methods:

What is good memory limit amount?

In general, 256MB is more than sufficient for any general PHP script to run properly. Scripts need more than that is usually heavy running script (eg. array looping, data scanning, backing up scripts) or scripts that take long time to process.

And also, badly PHP written scripts will consume more memory to process (because they’re bad!). As described above, malware PHP scripts will also take a lot of memory on your server.

So leaving the value too high isn’t really good in most cases, unless you’re doing some development works that needs to have everything unlocked to test your script’s capability, which is fine.

Again, we would suggest 256MB to be set for memory_limit , which is 256M, which will be more than enough.

Increasing PHP memory limit

The basic syntax on PHP memory limit value is:

…in which M is megabytes and G is gigabytes in memory.

// 128 MB memory_limit = 128M // 1 GB memory_limit = 1G

Below are 4 methods to increase (or decrease) PHP memory limit in details:

Читайте также:  Команда hex в python

1. Ask your host

Sounds funny right? But in most cases, if you use a good (and friendly) web hosting, just ask them to change value for you, and they’ll be more than happy to do that.

Most shared web hosting also have their own PHP memory allowance policy, so letting the provider adjust this value might also be more accurate than you doing it yourself.

2. Using php.ini

On Linux based operating systems, the file is usually located at /etc/ , so the full path is /etc/php.ini . You can open the file using any text editor, look for the setting name ( memory_limit , or more accurately, memory_limit = ), and replace the value after the = sign, to the value you wanted. Just remember to follow the syntax, M for megabyes and G for gigabytes.

Then, save the file and restart the Apache (or Nginx), and PHP-FPM services to make sure the new value is applied.

// Restarting Apache (Redhat based OS) systemctl restart httpd // Restarting Apache (Debian based OS) systemctl restart apache2 // Restarting Nginx systemctl restart nginx // Restarting PHP-FPM systemctl restart php-fpm

On Windows, locate the php.ini file inside your PHP executable folder, depends on the web application you’re using (standalone PHP installation, XAMPP, Laragon, etc.). Usually, the file is located in the same directory as the php.exe file. Then, replace the value of the memory_limit configuration inside the file, and restart the web service (Apache or Nginx).

The new PHP memory limit value will work in your PHP application after the services restarted.

3. Using .htaccess

To adjust PHP memory limit using .htaccess file, first, make sure the file is the one on the root directory of your website (because there can be multiple .htaccess files in sub-directories). Open the file in any text editor, add this line to the top of the file:

php_value memory_limit 256M

Save the file, and the setting will be applied immediately.
**This method might not work if you are using a shared web hosting and your provider disabled the ability to adjust PHP setting using .htaccess file.

4. Using ini_set in PHP file

Setting PHP memory limit by this method will only apply the configuration to a single file (the file that got edited), so it would be better to do this on a global configuration PHP file that gets included into other files in your PHP application framework.

Open the file, add this line to the top of the file, after

Then save the file, the setting will work immediately.
**This method might not work if you are using a shared web hosting and your provider disabled the ini_set function. Adding the @ symbol will hide the error in case the function could not be run.

Conclusion on PHP memory limit

Only give PHP the fair amount of memory it needs. 16MB is too low for nowadays PHP scripts, and also, it doesn’t make sense to give a 2GB of memory limit on a sever with only 1GB of ram. So we recommend 256MB setting value, which is good for most cases.

You might also like…

Источник

Оцените статью