PHP memory_limit 越多越好吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12858457/
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 04:21:16  来源:igfitidea点击:

Is the more the better for PHP memory_limit?

phpperformancememorymemory-limit

提问by u775856

In PHP, i am oftenly facing memory limit problem. Especially with highly resource integrated systems like Drupal, etc.

在 PHP 中,我经常面临内存限制问题。特别是对于像 Drupal 等资源高度集成的系统。

What i want to know here is:

我想知道的是:

  • Is it good to have very high php memory limit like 2GB?
  • Is there any major drawback?
  • 像 2GB 这样的非常高的 php 内存限制是不是很好?
  • 有什么大的缺点吗?

Edited:

编辑:

As a scenario, for example in Drupal, it NEEDS so much memory while we CLEAR the CACHE via Web Panel (Not by Drush or any script). So even for this case only, i am definitely needing high-limit around 512MB currently.

作为一个场景,例如在 Drupal 中,当我们通过 Web 面板(不是通过 Drush 或任何脚本)清除缓存时,它需要如此多的内存。因此,即使仅针对这种情况,我目前也绝对需要大约 512MB 的上限。

回答by Madara's Ghost

The golden rule: only give PHP what PHP needs to work, and nothing more.

黄金法则:只为 PHP 提供 PHP 需要的东西,仅此而已。

This will prevent situations when your system needlessly clogs on the PHP process, and will also assist you in finding bugs (A script that takes 1GB of memory is unusual, and giving a 2GB of memory limit will hide that).

这将防止您的系统不必要地阻塞 PHP 进程的情况,并且还将帮助您查找错误(占用 1GB 内存的脚本是不寻常的,而提供 2GB 的内存限制将隐藏它)。



For exceptional cases, where you know a single script file is very memory heavy, you can use ini_set()to change the memory limit directive at run-time. See thisand this.

对于特殊情况,您知道单个脚本文件占用大量内存,您可以ini_set()在运行时更改内存限制指令。看到这个这个

回答by GolezTrol

No, the more is not the better. It's the amount of memory a singlescript is allowed to consume. If your server contains, say, 4GB of memory, only two scripts simultaneously could eat all that memory.

不,不是越多越好。这是单个脚本允许消耗的内存量。如果您的服务器包含 4GB 内存,那么只有两个脚本可以同时占用所有内存。

In general, PHP scripts should use little memory. Normally all memory a script reserves is freed when it terminates, but you can clean up memory before that by explicitly unsetting variables that are no longer needed during execution. That way, you can make your script more memory-efficient.

一般来说,PHP 脚本应该使用很少的内存。通常,脚本在终止时保留的所有内存都会被释放,但您可以在此之前通过显式unsetting 执行期间不再需要的变量来清理内存。这样,您可以使脚本更节省内存。

回答by inquam

If you have scripts that you knowrequire large amounts of memory it's perfectly safe, and needed, to set the limit high. Problems can occur if the limit is set to high for the system in regards to available amount of memory and other running applications.

如果您有脚本,你知道需要大量的内存这是完全安全的,需要的,要设置限制高。如果系统的可用内存量和其他正在运行的应用程序的限制设置为高,则可能会出现问题。

Also keep in mind that a faulty script that might end up in an infinite loop processing some data would often come to a halt quite fast if the memory limit is 64 MB or something like that. Having the memory limit at 4 GB in this scenario would leave you with a system running at full tilt in an infinite loop for way longer and might cause your entire system to become unresponsive.

还请记住,如果内存限制为 64 MB 或类似内容,则可能会在无限循环中处理某些数据的错误脚本通常会很快停止。在这种情况下将内存限制为 4 GB 会使您的系统在无限循环中全速运行更长时间,并可能导致整个系统变得无响应。

But I have had large data crunching applications with memory limits of around 2-4 GB without any issues on systems with 4-8 GB of memory. Note that, as stated by other users, these applications was ONE instance. If you have applications that run several instances of your scripts (like most web-applications do) you will run into major problems by using a lot of memory in each instance. You will run out of memory and further clients will not be able to use your application until memory has been freed.

但是我有大量数据处理应用程序,内存限制约为 2-4 GB,在具有 4-8 GB 内存的系统上没有任何问题。请注意,正如其他用户所说,这些应用程序是一个实例。如果您的应用程序运行多个脚本实例(就像大多数 Web 应用程序一样),您将在每个实例中使用大量内存,从而遇到重大问题。您将耗尽内存,并且在释放内存之前,其他客户端将无法使用您的应用程序。

But don't just ramp up the memory limit to hide issues. Inspect why your application requires that much memory and see if you can rectify that first.

但不要只是增加内存限制来隐藏问题。检查为什么您的应用程序需要这么多内存,看看您是否可以先纠正它。