PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

4 ways to increase PHP memory limit

February 23, 2022
Reading Time: 4 mins read
PHP101.Net - How to increase PHP memory limit

PHP101.Net - How to increase PHP memory limit

Share on FacebookShare on Twitter

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

Assuming we want to change the PHP memory limit to 256MB (megabytes):

  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:
    memory_limit = 256M

    Then, restart both httpd and php-fpm service.

  3. In .htaccess file of the website’s root directory, add the following line:
    php_value memory_limit 256M

    Then just save the file.

  4. In the PHP file that you want to have memory limit increased, or the main include file, add this line to the top of the file:
    @ini_set('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:

3 ways to get PHP memory limit value

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:

[amount_in_integer][M/G]

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

For example:

// 128 MB
memory_limit = 128M

// 1 GB
memory_limit = 1G

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

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 <?php:

@ini_set('memory_limit',256M)

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…

  • How to write PHP comments (and best practices)
Tags: how much php memory limitincrease php memory limitphp memory limit
Previous Post

How to Write PHP Comments (and best practices)

Next Post

3 ways to get PHP memory limit value

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick solution
    • What is PHP memory limit?
    • How to get PHP memory limit?
    • What is good memory limit amount?
    • Increasing PHP memory limit
    • 1. Ask your host
    • 2. Using php.ini
    • 3. Using .htaccess
    • 4. Using ini_set in PHP file
    • Conclusion on PHP memory limit
    • You might also like…
Next Post
PHP101.net - Get PHP memory limit value

3 ways to get PHP memory limit value

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to install Composer on Linux

How to count array elements in PHP

How to download file from URL using PHP

How to change XAMPP PHP version on Windows

How to copy files using PHP

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.