search
HomeBackend DevelopmentPHP Tutorialini_set('memory_limit', '128M')、php.ini memory_limit引起的问题详细介绍

故障现象   


     在运行PHP程序,通常会遇到“Fatal Error: Allowed memory size of xxxxxx bytes exhausted”的错误, 这个意味着PHP脚本使用了过多的内存,并超出了系统对其设置的允许最大内存。

     解决这个问题,首先需要查看你的程序是否分配了过多的内存,在程序没有问题的情况下,你可以通过一下方法来增加PHP的内存限制(memory_limit)。


检查php的内存限制值

为了查看这个值,你需要建立一个空的php文件,比如view-php-info.php。然后将一下代码贴到里面。

<?php phpinfo(); ?>

    将这个脚本放到你的Web服务器上,然后在浏览器中调用它。这时你可以看到你的PHP环境配置的信息,其中有一部分是关于“memory_limit”的, 如下图:

    注:你可以用这种方法来查看php的其他参数设置,不仅仅是memory_limit


memory_limit应该设为多少?

    这个完全依赖于你的应用的要求。比如Wordpress,运行起核心代码需要32MB。Drupal 6则要求这个值最小为16MB,并推荐设置为32MB。如果你又安装不少的插件(plugins),尤其是那些要进行图像处理的模块,那么你可能需要128MB或更高的内存。


如何设置memory_limit


方法1: php.ini

最简单或常用的方法是修改php.ini

1.首先找到对你的网站生效的php.ini文件 由于有多个地方都可以设置php的参数,找到正确的配置文件,并进行更改是首先要做的一步。如果你上面的方法建立了php文件来查看其配置参数,则你可以找到“Loaded Configuration File”这一项,以下是个例子:


对于Linux用户,你可以通过执行“php -i | grep Loaded Configuration File”来找到对应的配置文件。而Windows用户,你可以尝试修改你的php安装目录下的php.ini。


2.编辑php.ini 在php.ini中,找到“memory_limit”这一项,如果没有,你可以在文件的尾部自己增加这个参数。以下是一些设置范例

memory_limit = 128M ; 可以将128M改为任何你想设置的值

保存文件

3.重启web 服务器 如果是web服务器使用Apache, 则执行:

httpd restart



有些情况下,你可能不被允许私修改php.ini。比如如果你购买了虚拟主机服务,但是你的服务商确禁止你修改这个文件。那么,你可以需要考虑用其他方法来增加memory_limit的值。

方法2: .htaccess

说明: 这种方法只有在php以Apache模块来执行时才生效。 在你的网站的根目录下找到“.htaccess”文件,如果没有,可以自己创建一个。然后把以下配置放入其中

php_value memory_limit 128M ; 可以将128M改为任何你想设置的值



方法3: 运行时修改php的内存设置

在你的php代码中增加以下命令行即可。

ini_set('memory_limit','128M');

memory_limit修改失败


如果你使用虚拟主机,有可能会出现memory_limit的值修改失败。这个需要联系你的服务商看怎么处理,通常他们限制了可以设置的最大值或者根本就不允许你修改。如果他们的环境真的无法满足你的要求,那么你可能要考虑换一个主机服务商。


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

生产环境下故障处理一例

现象说明:

    生产环境下,storage服务器上 没有安装任何服务(如apache,nginx,IIS等) 现在是纯命令行执行PHP 跑PHP脚本,在脚本的执行过程中发现(脚本内容含每次取5000个文件然后执行其它操作)速度奇慢,在本地测试时分分钟内搞定的事,在线上要执行2个多小时,经过排查排除掉服务器负载问题、数据库问题、服务器网络问题后,认为可能是php.ini配置文件中的资源限制所引起,于是修改了一下memory_limit将默认值128M修改为1024M后再次执行原脚本,速度得到大幅度提升,现把此参数的含义说明一下。


修改后需不需要重启?

    因为PHP没有守护进程一说,理论上你只要修改过以后,就会立即使用新的配置文件。


此值设置多大为好?

     建议根据应用需求来设置。物理服务器,如果内存够大的话建议限制内存较大一些如512M,1024M都可,如果是VPS之类的可能限制在128(默认)64M或者以下是比较合适的,特别是有web服务器的时候,一旦开启多个php的fast-cgi,估计服务器都卡的不动了。


参数语法

memory_limit integer

    本指令设定了一个脚本所能够申请到的最大内存字节数。

指令作用

    这有助于防止写得不好的脚本消耗光服务器上的可用内存。


在phpchina的论坛上,有人这样说:

    memory_limit是设置PHP使用内存时空间,一般设置为内存的四分之一,应该就差不多了。如512M内存设置128M,1G内存设置256M。

    设置太大会影响系统速度,因为系统和数据库及其他程序同样需要内存空间,一般系统和数据库内存空间是自己分配的。


附三种修改方法

可能是分词程序的问题。只要搜索的字段达到十个汉字以上,就会出现诸如以下的错误

Fatal error: Allowed memory size of 2345643 bytes exhausted

上网找了方法。有3种办法。

1.直接修改PHP.INI

memory_limit = 16M  ;

但是我修改了没有用。据说是要重启服务器的。但是很显然。我的 是虚拟主机。所以有独立主机的可以这样修改。

2.修改.htaccess

php_value memory_limit 16M

修改后直接出现500错误。可能是主机商限制

3.直接在程序页面上修改。

ini_set(’memory_limit’, ‘16M‘);

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.