简体   繁体   English

当 memory_limit 设置为 -1 时,PHP memory 有什么限制?

[英]What limits PHP memory when memory_limit is set to -1?

Background背景

I understand how PHP's memory_limit setting can be used to control how much memory is available for PHP to use.我了解如何使用 PHP 的memory_limit设置来控制 PHP 可以使用多少 memory。

As well as using this property to raise/lower the memory limit for your script, you can also set it to -1 to disable the memory limit altogether.除了使用此属性来提高/降低脚本的 memory 限制外,您还可以将其设置为-1以完全禁用 memory 限制。

However, as we all know, a computer does not have infinite memory , therefore all we are really talking about is removing any self-imposed limits implemented by PHP itself.但是,众所周知,计算机没有无限的 memory ,因此我们真正谈论的是消除 PHP 本身实现的任何自我施加的限制。

An illustration插图

We can demonstrate that this is true, by using the following script:我们可以通过使用以下脚本来证明这是真的:

<?php

    print("Original: ");
    print(ini_get('memory_limit'));

    ini_set('memory_limit', -1);

    print(", New: ");
    print(ini_get('memory_limit'));

    $x = "123456789ABCDEF";
    while (true)
        $x .= $x;

?>

When running from the command-line, I get the following output:从命令行运行时,我得到以下 output:

Original: 128M, New: -1原始:128M,新:-1

PHP Fatal error: Out of memory (allocated 503840768) (tried to allocate 1006632961 bytes) in test.php on line 14 PHP 致命错误:超出 memory(已分配 503840768)(试图分配 1006632961 字节)在 test.ZE1BFD762321E41961CEE4AC0B6E 4 行

Fatal error: Out of memory (allocated 503840768) (tried to allocate 1006632961 bytes) in test.php on line 14致命错误:在测试中超出 memory(分配 503840768)(试图分配 1006632961 字节)。第 14 行的 php

zend_mm_heap corrupted zend_mm_heap 损坏

And from the web (via Apache) I get something similar:从 web(通过 Apache)我得到类似的东西:

Original: 128M, New: -1原始:128M,新:-1

Fatal error: Out of memory (allocated 503840768) (tried to allocate 1006632961 bytes) in test.php on line 14致命错误:在测试中超出 memory(分配 503840768)(试图分配 1006632961 字节)。第 14 行的 php

In my examples, the values are the same (~480MB) so it doesn't appear that the web server is imposing a limit.在我的示例中,这些值是相同的 (~480MB),因此 web 服务器似乎没有施加限制。 Also, this is nowhere near the amount of RAM installed in the system (even ignoring virtual memory) so it is not a hardware limitation.此外,这远不及系统中安装的 RAM 量(即使忽略虚拟内存),因此它不是硬件限制。

Note that these tests were run on PHP 5.6 on a Windows machine with 4GB of RAM.请注意,这些测试是在具有 4GB RAM 的 Windows 机器上的 PHP 5.6 上运行的。 However, output is similar on other PHP versions .但是,output与其他 PHP 版本类似

Finally, we come to a question!最后,我们来一个问题!

Given the above:鉴于上述情况:

  1. What actually dictates the memory limit when we set it to -1 ?当我们将其设置为-1时,实际上是什么决定了 memory 限制?
  2. Is there a way of finding out what this limit is from within PHP?有没有办法从 PHP 中找出这个限制是什么?

When you set memory_limit to -1 the actual limit is memory available to operating system.当您将memory_limit设置为-1时,实际限制为操作系统可用的 memory。

By available memory I mean sum of physical memory(RAM) and virtual memory(SWAP) minus memory currently used by others processes, and operating system itself.可用的 memory 是指物理内存 (RAM) 和虚拟内存 (SWAP) 的总和减去其他进程和操作系统本身当前使用的 memory。 This is hardware limitation, you just have to consider other processes as well.这是硬件限制,您只需要考虑其他进程。

For example given 4GB of RAM, no SWAP, and other processes(DB, WebBrowser, etc.) consuming 3GB of memory, you have ~1GB available to be used by PHP.例如,给定 4GB 的 RAM、无 SWAP 和其他进程(数据库、WebBrowser 等)消耗 3GB 的 memory,您有大约 1GB 可供 PHP 使用。 When you try to use more operating system rejects memory allocation request, and subsequently PHP exits with "out of memory" error.当您尝试使用更多操作系统时拒绝 memory 分配请求,随后 PHP 以“内存不足”错误退出。


There is no method which can be used across different operating systems, PHP don't provide functions to check how much memory is available and used in the environment.没有可以跨不同操作系统使用的方法,PHP 不提供检查有多少 memory 在环境中可用和使用的功能。 If you know your application is running on Linux you can try to read memory info from /proc/meminfo .如果您知道您的应用程序正在 Linux 上运行,您可以尝试从/proc/meminfo读取 memory 信息。

<?php
echo file_get_contents("/proc/meminfo");

which will output something like:这将 output 类似于:

MemTotal:        1999084 kB
MemFree:          174344 kB
MemAvailable:    1077264 kB
Buffers:          151328 kB
Cached:           726752 kB
...

Based on that you can try to estimate amount of available memory, be advised memory used by other processes will vary with time.基于此,您可以尝试估计可用 memory 的数量,请注意其他进程使用的 memory 会随时间而变化。

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

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