温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP中怎么利用memory_limit限制进程的内存

发布时间:2021-06-23 17:28:33 来源:亿速云 阅读:415 作者:Leah 栏目:编程语言

PHP中怎么利用memory_limit限制进程的内存,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

memory_limit 顾名思义,即限制 PHP 进程对于内存的使用。例如:

magento2 的系统要求里有关于 PHP memory_limit 的限制,不能低于 512M。(默认值为 128M, 如果不更改,会导致 magento 的后台处理逻辑无法正常执行)

看一下 PHP 官网的解释

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.

需要注意的是,memory_limit 的值越高,即单个 PHP 进程占用的内存越多,系统能够并发处理的请求越少。例如,一个 2G 内存的机器

memory_limit 设为 128M, 则同时最多能处理 16 个请求memory_limit 设为 256M, 则同时最多能处理 8 个请求memory_limit 设为 512M, 则同时最多能处理 4 个请求

memory_limit 的值是越大越好么?

当然不是,memory_limit 主要是为了防止程序 bug, 或者死循环占用大量的内存,导致系统宕机。在引入大量三方插件,或者代码时,进行内存限制就非常有必要了。

memory_limit 会使每个 PHP process 都占用固定的内存?

还是仅仅为分配内存的上限?测试一下

思路,memory_limit 设置为 10M, PHP 请求中初始化一个 2M/20M 的字符串,看看系统进程中内存的占用情况。

Nginx 配置

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 测试文件

<?php
$char_count = 2;
$M = 1024 * 1024;
echo sprintf("Current memory_limit value is: %s.", ini_get('memory_limit'));
echo sprintf('<br/>Amount of memory allocated to PHP: %0.3fM.', memory_get_usage() / $M);
$s = str_repeat("a", $M * $char_count);
//sleep(30);
echo sprintf('<br/>Amount of memory allocated to PHP: %0.3fM.', memory_get_usage() / $M);
echo sprintf('<br/>Total memory allocated from system: %0.3fM.', memory_get_usage($real_usage=true) / $M);
echo '<br/>success';

测试结果

$char_count 为 2 时,即初始化一个占用内存 2M 的字符串,输出结果为

Current memory_limit value is: 10M.
Amount of memory allocated to PHP: 0.344M.
Amount of memory allocated to PHP: 2.348M.
Total memory allocated from system: 4.004M.
success

$char_count 为 20 时,即初始化一个占用内存 20M 的字符串,输出结果为

Current memory_limit value is: 10M.
Amount of memory allocated to PHP: 0.346M.

注意,HTTP 状态码为 500, 也就是说执行到字符串初始化的时候,PHP 进程被系统干掉了。

看一下 Nginx error.log 中记录的日志信息

2017/03/01 10:41:23 [error] 6903#6903: *39 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Allowed memory size of 10485760 bytes exhausted (tried to allocate 20971552 bytes) in /home/zhongwei/work/test/memory_limit/index.php on line 8
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /home/zhongwei/work/test/memory_limit/index.php:0
PHP message: PHP   2. str_repeat() /home/zhongwei/work/test/memory_limit/index.php:8" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "localhost:8093"

实际测试结果说明,memory_limit 只是限制了每个 PHP 进程的内存占用上限,而不是为每个进程分配了固定的内存。所以,并不会因为 memory_limit 设置越大,导致并发数出现降低。

memory_limit 的默认值为多少

  • PHP 5.2 之前为 8M

  • PHP 5.2 为 16M

  • PHP 5.2 之后的版本为 128M

看完上述内容,你们掌握PHP中怎么利用memory_limit限制进程的内存的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI