Ini_set ('memory _ limit ', '128m') and php. inimemory_limit

Source: Internet
Author: User
Problems caused by ini_set (memory_limit, 128 M) and php. inimemory_limit


When running the PHP program, the Error "Fatal Error: Allowed memory size of xxxxxx bytes exhausted" usually occurs. This means that the PHP script uses too much memory, the maximum memory allowed by the system is exceeded.

To solve this problem, first check whether your program has allocated too much memory. if there is no problem with the program, you can use the following method to increase the memory limit (memory_limit) of PHP ).


Check the php memory limit value

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

 

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

Note: You can use this method to view other php parameter settings, not just memory_limit


What should memory_limit be set?

This is totally dependent on your application requirements. For example, Wordpress requires 32 MB to run the core code. Drupal 6 requires a minimum value of 16 MB and is recommended to be set to 32 MB. If you install many plug-ins, especially those that require image processing, you may need MB or higher memory.


How to set memory_limit


Method 1: php. ini

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

1. First, find the php. ini file that takes effect for your website. because php parameters can be set in multiple places, find the correct configuration file and make changes. If the above method creates a php File to view its Configuration parameters, you can find the "Loaded Configuration File" item. The following is an example:


For Linux users, you can find the corresponding Configuration File by executing "php-I | grep Loaded Configuration File. For Windows users, you can try to modify the php. ini under your php installation directory.


2. edit php. ini. in php. ini, find "memory_limit". if not, add this parameter at the end of the file. The following are some configuration examples:

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

Save files

3. restart the web server. if the web server uses Apache, run the following command:

Httpd restart



In some cases, you may not be allowed to modify php. ini. For example, if you have purchased the VM service, but your service provider does not allow you to modify this file. You can use other methods to increase the value of memory_limit.

Method 2:. htaccess

Note: This method takes effect only when php is executed using the Apache module. Find the ". htaccess" file in the root directory of your website. if not, create one by yourself. Then put the following configuration into it:

Php_value memory_limit 128 M; you can change M to any value you want to set.



Method 3: modify the php memory settings at runtime

Add the following command line to your php code.

Ini_set ('memory _ limit ', '128m ');

Memory_limit modification failed


If you use a VM, you may fail to modify the memory_limit value. You need to contact your service provider to learn how to handle this problem. Generally, they limit the maximum value that can be set or they cannot be modified at all. If their environment does not meet your requirements, you may have to consider changing the host service provider.


++ ++

Fault handling in production environment

Symptom description:

In the production environment, no services (such as apache, nginx, and IIS) are installed on the storage server. Currently, PHP scripts are run in pure command lines, during the script execution process, it was found that (the script content contains 5000 files each time and then other operations were executed) the speed was very slow, and it was done within minutes during local testing, it takes more than two hours to run online. After troubleshooting the server load, database, and server network problems, php may be considered as the cause. the resource limit in the ini configuration file is caused by modifying memory_limit to change the default value of M to M and then execute the original script again. The speed is greatly improved. the meaning of this parameter is described.


Do I need to restart after modification?

Because PHP does not have a daemon, theoretically you only need to modify it and then use the new configuration file immediately.


How high is this value set?

We recommend that you set it based on your application requirements. For physical servers, if the memory is large enough, it is recommended to limit the memory to a larger value, for example, 512 MB or 128 MB. for VPS and other servers, the limit may be (default) 64 MB or below, especially when there is a web server, once the fast-cgi of multiple php is enabled, it is estimated that the server will not be stuck.


Parameter syntax

Memory_limit integer

This command sets the maximum number of memory bytes that a script can apply.

Directive function

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


On the phpchina Forum, someone said:

Memory_limit is used to set PHP to use the memory space-time window. generally, it is set to 1/4 of the memory, which should be similar. For example, the 128 M memory is set to 256 M, and the 1G memory is set to M.

Setting too much affects the system speed, because the system and the database and other programs also need memory space. Generally, the system and database memory space are allocated by themselves.


Three modification methods are provided.

It may be a problem with the word splitting program. As long as the search field contains more than 10 Chinese characters, the following errors may occur:

Fatal error: Allowed memory size of 2345643 bytes exhausted

I found a method for surfing the Internet. There are three methods.

1. directly modify PHP. INI

Memory_limit = 16 M;

But it is useless if I modify it. It is said that the server is to be restarted. But obviously. I am a virtual host. Therefore, if you have an independent host, you can modify it like this.

2. Modify. htaccess

Php_value memory_limit 16 M

After modification, error 500 is displayed. It may be a host provider restriction.

3. modify it directly on the program page.

Ini_set ('memory _ limit ', '16m ');

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.