简体   繁体   English

PHP memory_limit是否为PHP预分配内存

[英]Does PHP memory_limit pre-allocate memory to PHP

Some of the pages on our website a hitting the PHP memory limit, which is currently set to 128MB. 我们网站上的一些页面达到了PHP内存限制,目前设置为128MB。 When requesting to bump the memory limit to 256MB, we were told by the engineer at our hosting company that: 当请求将内存限制提高到256MB时,我们的托管公司的工程师告诉我们:

On our server "the average request requires less than 64MB of RAM" 在我们的服务器上“平均请求需要少于64MB的RAM”

and

"shifting memory limit to 256MB essentially creates an average allocation of 192M of memory that won't get used on most requests" “将内存限制转移到256MB实际上会创建192M内存的平均分配,而不会在大多数请求中使用”

Is this correct that the memory_limit amount is actually pre-allocated and is 192MB will be wasted for most of our requests? 这是正确的,memory_limit数量实际上是预先分配的,对于我们的大多数请求,是否会浪费192MB?

Thank you! 谢谢!

From: http://php.net/manual/en/ini.core.php#ini.memory-limit 来自: http//php.net/manual/en/ini.core.php#ini.memory-limit

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. 请注意,要没有内存限制,请将此指令设置为-1。

In my experience, many sites require more than 128M. 根据我的经验,许多网站需要超过128M。

No it does not. 不,不是的。 It sets the max that a script is allowed to allocate. 它设置允许脚本分配的最大值。 It doesn't allocate by itself. 它不会自行分配。 Test it with the php memory_get_usage function. 使用php memory_get_usage函数测试它。

source 资源

No. You can't pre-allocate memory with memory_limit. 不可以。您无法使用memory_limit预分配内存。

See this post as a reference as well: How do I pre-allocate memory for an array in PHP? 请参阅这篇文章作为参考: 如何在PHP中为数组预先分配内存?

Maybe he meant that by bumping the limit to 256M globally, the average request went to 192M. 也许他的意思是,通过在全球范围内将限制提高到256M,平均请求达到了192M。 This would mean that there are a lot of people hitting the absurdly low 64M limit. 这意味着有很多人达到荒谬的低64M限制。

Point on PHP memory limit is "prevent poorly written scripts for eating up all available memory on a server", not "pre-allocate a memory size". 关于PHP内存限制的要点是“防止编写糟糕的脚本以占用服务器上的所有可用内存”,而不是“预先分配内存大小”。

For example from PHP5.6 source It's simply a hard cap that's enforced as needed - or another words "Maximum amount of memory a script may consume". 例如,从PHP5.6源代码它只是一个根据需要强制执行的硬帽 - 或者说“脚本可能消耗的最大内存量”。

zend_mm_safe_error(heap, "Allowed memory size of %ld bytes exhausted (tried to allocate %ld bytes)", heap->limit, size);

REF: http://jpauli.github.io/2014/07/02/php-memory.html 参考: http//jpauli.github.io/2014/07/02/php-memory.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM