php 已用完 X 字节的允许内存大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4096582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:56:22  来源:igfitidea点击:

Allowed memory size of X bytes exhausted

phpmemorymemory-managementout-of-memorymemory-limit

提问by Nathan

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 13965430 bytes)

致命错误:允许的内存大小为 67108864 字节已用完(尝试分配 13965430 字节)

PHPInfo shows that I have a memory_limit of 128M, so I'm confused as to why the error says I only have 64M. Is it possible for phpinfo to report incorrectly? Or for PHP to use two separate php.inis?

PHPInfo 显示我的 memory_limit 为 128M,所以我很困惑为什么错误说我只有 64M。phpinfo 有没有可能报错?还是让 PHP 使用两个独立的 php.inis?

The error was being caused by an ini_set call in one of the primary php files that a co-worker of mine added without my knowledge.

该错误是由我的同事在我不知情的情况下添加的主要 php 文件之一中的 ini_set 调用引起的。

采纳答案by Marc B

PHP's config can be set in multiple places:

PHP 的配置可以在多个地方设置:

  1. master system php.ini(usually in /etc somewhere)
  2. somewhere in Apache's configuration (httpd.conf or a per-site .conf file, via php_value)
  3. CLI & CGI can have a different php.ini(use the command php -i | grep memory_limitto check the CLI conf)
  4. local .htaccess files (also php_value)
  5. in-script (via ini_set())
  1. 主系统php.ini(通常在 /etc 某处)
  2. Apache 配置中的某处(httpd.conf 或每个站点的 .conf 文件,通过php_value
  3. CLI & CGI 可以有不同php.ini(使用命令php -i | grep memory_limit检查 CLI conf)
  4. 本地 .htaccess 文件(也php_value
  5. 脚本内(通过ini_set()

In PHPinfo's output, the "Master" value is the compiled-in default value, and the "Local" value is what's actually in effect. It can be either unchanged from the default, or overridden in any of the above locations.

在 PHPinfo 的输出中,“Master”值是编译后的默认值,“Local”值是实际生效的值。它可以与默认值保持不变,也可以在上述任何位置覆盖。

Also note that PHP generally has different .ini files for command-line and webserver-based operation. Checking phpinfo()from the command line will report different values than if you'd run it in a web-based script.

另请注意,PHP 通常具有不同的 .ini 文件用于命令行和基于 Web 服务器的操作。phpinfo()从命令行检查将报告与在基于 Web 的脚本中运行它不同的值。

回答by ?ark? dinle

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

or

或者

php.ini  =>  memory_limit = 128M

or

或者

php_value memory_limit 128M

回答by Somnath Muluk

I had same issue. I found the answer:

我有同样的问题。我找到了答案:

ini_set('memory_limit', '-1');

Note: It will take unlimited memory usage of server.

注意:这将占用服务器的无限内存使用量。

Update:Use this carefully as this might slow down your system if the PHP script starts using an excessive amount of memory, causing a lot of swap space usage. You can use this if you know program will not take much memory and also you don't know how much to set it right now. But you will eventually find it how much memory you require for that program.

更新:小心使用它,因为如果 PHP 脚本开始使用过多的内存,这可能会减慢您的系统速度,从而导致大量的交换空间使用。如果您知道程序不会占用太多内存,并且您现在不知道要设置多少内存,则可以使用它。但是您最终会发现该程序需要多少内存。

You should always memory limit as some value as answered by @?ark? dinle.

您应该始终将内存限制为 @ 回答的某个值?ark? dinle

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

Giving unlimited memory is bad practice, rather we should give some max limit that we can bear and then optimise our code or add some RAMs.

提供无限内存是不好的做法,我们应该给出一些我们可以承受的最大限制,然后优化我们的代码或添加一些 RAM。

回答by oussaka

The memory must be configured in several places.
Set memory_limitto 512M:

内存必须在几个地方配置。
设置memory_limit为 512M:

sudo vi /etc/php5/cgi/php.ini
sudo vi /etc/php5/cli/php.ini
sudo vi /etc/php5/apache2/php.ini Or /etc/php5/fpm/php.ini

Restart service:

重启服务:

sudo service service php5-fpm restart
sudo service service nginx restart

or

或者

sudo service apache2 restart

Finally it should solve the problem of the memory_limit

最后应该可以解决问题 memory_limit

回答by bcosca

If you're sure you restarted Apache after configuring php.ini, then you might be looking at the wrong php.ini file

如果您确定在配置 php.ini 后重新启动了 Apache,那么您可能正在查看错误的 php.ini 文件

回答by terwxqian

1 check current limit:
(in my os)php -i | grep limit  => memory_limit => 256M => 256M

2  locate php.ini 
php --ini =>   
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/curl.ini 
...

3 change memory_limit in php.ini
vi /etc/php.ini
memory_limit = 512M

4 restart nginx and (php-fpm if being used)
service php-fpm restart
service nginx restart

回答by user3806549

If by increasing the memory limit you have gotten rid of the error and your code now works, you'll need to take measures to decrease that memory usage. Here are a few things you could do to decrease it:

如果通过增加内存限制您已经摆脱了错误并且您的代码现在可以工作,您将需要采取措施来减少内存使用量。以下是您可以采取的一些措施来减少它:

If you're reading files, read them line-by-line instead of reading in the complete file into memory. Look at fgets and SplFileObject::fgets. Upgrade to a new version of PHP if you're using PHP 5.3. PHP 5.4 and 5.5 use much less memory.

如果您正在读取文件,请逐行读取它们,而不是将整个文件读入内存。查看 fgets 和 SplFileObject::fgets。如果您使用的是 PHP 5.3,请升级到新版本的 PHP。PHP 5.4 和 5.5 使用更少的内存。

Avoid loading large datasets into in an array. Instead, go for processing smaller subsets of the larger dataset and, if necessary, persist your data into a database to relieve memory use.

避免将大型数据集加载到数组中。相反,处理较大数据集的较小子集,并在必要时将数据保存到数据库中以减少内存使用。

Try the latest version or minor version of a third-party library (1.9.3 vs. your 1.8.2, for instance) and use whichever is more stable. Sometimes newer versions of libraries are written more efficiently.

尝试第三方库的最新版本或次要版本(例如,1.9.3 与您的 1.8.2)并使用更稳定的版本。有时,较新版本的库的编写效率更高。

If you have an uncommon or unstable PHP extension, try upgrading it. It might have a memory leak.

如果您有一个不常见或不稳定的 PHP 扩展,请尝试升级它。它可能有内存泄漏。

If you're dealing with large files and you simply can't read it line-by-line, try breaking the file into many smaller files and process those individually. Disable PHP extensions that you don't need.

如果您正在处理大文件并且您根本无法逐行读取它,请尝试将文件分成许多较小的文件并单独处理它们。禁用不需要的 PHP 扩展。

In the problem area, unset variables which contain large amounts of data and aren't required later in the code.

在问题区域中,未设置包含大量数据且在代码后面不需要的变量。

FROM: https://www.airpair.com/php/fatal-error-allowed-memory-size

来自:https: //www.airpair.com/php/fatal-error-allowed-memory-size

回答by heySushil

This problem is happend because of php.ini defined limit was exided but have lot's of solution for this but simple one is to find your local servers folder and on that find the php folder and in that folder have php.ini file which have all declaration of these type setups. You just need to find one and change that value. But in this situation have one big problem once you change in your localhost file but what about server when you want to put your site on server it again produce same problem and you again follow the same for server. But you also know about .htaccess file this is one of the best and single place to do a lot's of things without changing core files. Like you change www routing, removing .php or other extentions from url or add one. Same like that you also difine default values of php.ini here like this - First you need to open the .htaccess file from your root directory or if you don't have this file so create one on root directory of your site and paste this code on it -

这个问题的发生是因为 php.ini 定义的限制被排除,但有很多解决方案,但简单的一个是找到你的本地服务器文件夹,然后找到 php 文件夹,在该文件夹中有 php.ini 文件,其中包含所有声明这些类型的设置。您只需要找到一个并更改该值。但是在这种情况下,一旦您更改了 localhost 文件,就会遇到一个大问题,但是当您想将站点放在服务器上时,服务器又会产生相同的问题,并且您再次对服务器执行相同的操作。但是您也知道 .htaccess 文件,这是在不更改核心文件的情况下完成很多事情的最佳且单一的地方之一。就像您更改 www 路由,从 url 中删除 .php 或其他扩展名或添加一个一样。同样,您还可以定义 php 的默认值。

php_value upload_max_filesize 1000M
php_value post_max_size 99500M
php_value memory_limit 500M
php_value max_execution_time 300

after changes if you want to check the changes just run the php code

更改后,如果您想检查更改,只需运行 php 代码

<?php phpinfo(); ?> 

it will show you the php cofigrations all details. So you find your changes.

它将向您显示 php cofigrations 的所有详细信息。所以你会发现你的变化。

Note: for defining unlimited just add -1 like php_value memory_limit -1 It's not good and most of the time slow down your server. But if you like to be limit less then this one option is also fit for you. If after refresh your page changes will not reflect you must restart your local server once for changes.

注意:要定义无限制,只需添加 -1 就像 php_value memory_limit -1 这不好,而且大多数时候会降低服务器的速度。但是,如果您想减少限制,那么这个选项也适合您。如果刷新后您的页面更改不会反映您必须重新启动本地服务器一次以进行更改。

Good Luck. Hope it will help. Want to download the .htaccess file click this.

祝你好运。希望它会有所帮助。 想要下载 .htaccess 文件点击这里。