Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use memory_limit to limit the memory usage of PHP processes

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to use memory_limit to limit the memory use of the PHP process, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Memory_limit, as its name implies, restricts the use of memory by PHP processes. For example:

There is a limit on PHP memory_limit in the system requirements of magento2, which cannot be less than 512m. (the default value is 128m, which will cause the background processing logic of magento to fail to execute normally if it is not changed.)

Take a look at the explanation on PHP's official website.

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to-1.

It is important to note that the higher the value of memory_limit, the more memory a single PHP process takes up, and the fewer requests the system can process concurrently. For example, a machine with 2G memory

If memory_limit is set to 128m, up to 16 requests can be processed at the same time.

If memory_limit is set to 256m, a maximum of 8 requests can be processed at the same time

If memory_limit is set to 512m, a maximum of 4 requests can be processed at the same time

Is the higher the value of memory_limit, the better?

Of course not, memory_limit is mainly to prevent the program bug, or endless loop takes up a lot of memory, resulting in system downtime. Memory restrictions are necessary when introducing a large number of tripartite plug-ins, or code.

Does memory_limit make each PHP process take up fixed memory?

Or is it just the upper limit for allocating memory? Test it

The idea is that memory_limit is set to 10m, and a string of 2M/20M is initialized in the PHP request to see how much memory is being used in the system process.

Nginx configuration

Server {

Listen 8093

Root / home/zhongwei/work/test/memory_limit/

Index index.php index.html index.htm

Server_name localhost

Location / {

# try_files $uri $uri/ = 404

Try_files $uri $uri/ / index.php?q=$uri&$args

}

Location ~\ .php$ {

Include snippets/fastcgi-php.conf

Fastcgi_param PHP_VALUE "memory_limit = 10m"

Fastcgi_pass unix:/run/php/php7.0-fpm.sock

}

}

PHP test file

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report