如何使用memory_limit限制PHP进程的内存使用

醉折花枝作酒筹
发布: 2021-06-23 16:24:39
转载
4102人浏览过

虚拟主机php中memory_limit是php单个脚本单次执行最大可用内存限制;默认限制为256mb,最大可调整为512mb。下面由小编介绍使用memory_limit限制php进程的内存使用方法。

如何使用memory_limit限制PHP进程的内存使用

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

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

看一下 PHP 官网的解释

立即学习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 测试文件

AppMall应用商店
AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店56
查看详情 AppMall应用商店
<?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限制PHP进程的内存使用的详细内容,更多请关注php中文网其它相关文章!

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:大象笔记网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号