Ini_set (' Memory_limit ', ' 128M '), php.ini memory_limit problems caused by the detailed introduction

Source: Internet
Author: User
Tags save file web hosting
Failure phenomena


In Running a PHP program, you will typically encounter the "Fatal error:allowed memory size of xxxxxx bytes exhausted" error, which means that the PHP script uses too much memory and exceeds the maximum allowable memory set by the system.

To solve this problem, you need to check whether your program is allocating too much memory, and you can increase the PHP memory limit (memory_limit) by a few methods if there is no problem with the program.


Check PHP for Memory limit values

To view this value, you need to create an empty PHP file, such as view-php-info.php. Then paste the code inside.

 
  

Put the script on your Web server and call it in the browser. At this point you can see your PHP environment configuration information, some of which are about "memory_limit", such as:

Note: You can use this method to view PHP's other parameter settings, not just Memory_limit


How much should the Memory_limit be set?

This depends entirely on the requirements of your application. such as WordPress, running the core code needs 32MB. Drupal 6 requires a minimum value of 16MB and is recommended to be set to 32MB. If you install a lot of plugins (plugins), especially those for image processing, you may need 128MB or higher memory.


How to set Memory_limit


Method 1:php.ini

The simplest or most common method is to modify the php.ini

1. First find the php.ini file that takes effect on your website because there are multiple places where you can set PHP parameters, finding the correct profile, and making changes is the first step. If you have built a PHP file in the above method to see its configuration parameters, you can find the "Loaded configuration file", the following is an example:


For Linux users, you can do this by executing the Php-i | grep Loaded configuration File "to locate the corresponding profile. While Windows users, you can try to modify the php.ini in your PHP installation directory.


2. Edit PHP.ini in php.ini, find the "Memory_limit" this item, if not, you can add this parameter at the end of the file itself. Here are some examples of settings

Memory_limit = 128M; You can change the 128M to any value you want to set

Save File

3. Restart the Web server if the Web server is using Apache, execute:

httpd restart



In some cases, you may not be allowed to modify php.ini privately. For example, if you purchase a web hosting service, your provider does not allow you to modify the file. Then, you may want to consider other ways to increase the value of the Memory_limit.

Method 2:. htaccess

Note: This method only takes effect if PHP is executed with the Apache module. Find the ". htaccess" file at the root of your website, and if not, you can create one yourself. Then put the following configuration into it

Php_value Memory_limit 128M; You can change the 128M to any value you want to set



Method 3: Modify the PHP memory settings at run time

Add the following command line to your PHP code.

Ini_set (' Memory_limit ', ' 128M ');

Memory_limit modification failed


If you use a virtual host, it is possible that the Memory_limit value modification fails. This need to contact your service provider to see how to handle, usually they limit the maximum value can be set or do not allow you to modify at all. If their environment really does not meet your requirements, then you may want to consider changing a host service provider.


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A case of fault treatment in production environment

Description of the phenomenon:

Production environment, storage server does not have any services installed (such as APACHE,NGINX,IIS, etc.) is now a pure command line to execute PHP run PHP script, found during the execution of the script (the script content with each fetch 5,000 files and then perform other operations) is very slow, Within minutes of local testing done, on-line to perform 2 hours, after troubleshooting the server load problem, database problems, server network problems, it may be the php.ini configuration file in the resource constraints caused, so modified Memory_ Limit changes the default value of 128M to 1024M and then executes the original script again, the speed has been greatly improved, now the meaning of this parameter is explained.


Do I need to restart after the modification?

Because PHP does not have a daemon, it is theoretically possible to use the new configuration file as soon as you have modified it.


How big is this value set to good?

It is recommended to set it according to your application needs. Physical server, if the memory is big enough, it is recommended to limit the memory larger, such as 512m,1024m can be, if it is a VPS and the like may be limited to 128 (the default) 64M or the following is more appropriate, especially when there is a Web server, once opened multiple PHP fast-cgi, It is estimated that the server is stuck.


Parameter syntax

Memory_limit integer

This directive sets the maximum number of bytes of memory that a script can request.

instruction function

This helps prevent poorly written scripts from consuming the available memory on the optical server.


In Phpchina's forum, it was said that:

Memory_limit is to set PHP to use memory space, generally set to One-fourth of memory, should be almost. such as 512M memory settings 128m,1g memory settings 256M.

Set too General assembly affects the system speed, because the system and the database and other programs also need memory space, the general system and database memory space is allocated by itself.


Three methods of modification are included

It may be a problem with the word breaker program. As long as the search field reaches 10 characters or more, errors such as the following will occur

Fatal error:allowed memory size of 2345643 bytes exhausted

Find a way to surf the internet. There are 3 ways.

1. Modify PHP.ini directly

Memory_limit = 16M;

But I changed it to no use. It is said to restart the server. But it's obvious. My is a virtual host. So there are independent hosts that can be modified like this.

2. Modify. htaccess

Php_value Memory_limit 16M

The 500 error occurs directly after the modification. May be a host vendor limit

3. Modify it directly on the program page.

Ini_set (' Memory_limit ', ' 16M ');

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.