首页 > 解决方案 > PHP内存不足与允许的内存大小耗尽

问题描述

我需要有关 PHP 内存问题的帮助。Composer 和 Phan 在运行 cygwin php 7.3.7 的 Windows 10 系统上均因内存不足错误而失败。

在此处输入图像描述

我的 memory_limit 设置为 2G

$ php -v
PHP 7.3.7 (cli) (built: Jul 21 2019 18:10:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.9.4, Copyright (c) 2002-2020, by Derick Rethans

$ php -i | grep memory
memory_limit => 2G => 2G
Collecting memory statistics => No

我有一个简单的程序来测试内存限制:

<?php
echo "Memory test...\n";
$argv = $_SERVER['argv'];
$step = $argv[1] * 1024 * 1024;
$a    = [];
while (1)
{
  echo "  ". memory_get_usage(true). " bytes used.  Allocating another ~$step bytes\n";
  $a[] = str_repeat('a', $step);
}

当我运行php -f tmp/t.php 7它时,它可以预见地会在我的 2G 限制处出现“允许的内存大小已用完”错误的内存不足:

$ php -f tmp/t.php 7
Memory test...
  2097152 bytes used.  Allocating another ~7340032 bytes
  9502720 bytes used.  Allocating another ~7340032 bytes
  16908288 bytes used.  Allocating another ~7340032 bytes
 ...
  2105278464 bytes used.  Allocating another ~7340032 bytes
  2112684032 bytes used.  Allocating another ~7340032 bytes
  2120089600 bytes used.  Allocating another ~7340032 bytes
  2127495168 bytes used.  Allocating another ~7340032 bytes
  2134900736 bytes used.  Allocating another ~7340032 bytes
  2142306304 bytes used.  Allocating another ~7340032 bytes

Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 7340064 bytes) in /cygdrive/c/Users/dschmidt/tmp/t.php on line 13

Call Stack:
    0.0005     403664   1. {main}() /cygdrive/c/Users/dschmidt/tmp/t.php:0
    0.8291 2140640320   2. str_repeat() /cygdrive/c/Users/dschmidt/tmp/t.php:13

但是当我用它运行它时,php -f tmp/t.php 8在分配了区区的 10M 后,它很早就因“内存不足”错误而死:

$ php -f tmp/t.php 8
Memory test...
  2097152 bytes used.  Allocating another ~8388608 bytes
  10551296 bytes used.  Allocating another ~8388608 bytes

Fatal error: Out of memory (allocated 10551296) (tried to allocate 8388640 bytes) in /cygdrive/c/Users/dschmidt/tmp/t.php on line 13

Call Stack:
    0.0005     403664   1. {main}() /cygdrive/c/Users/dschmidt/tmp/t.php:0
    0.0055    8858240   2. str_repeat() /cygdrive/c/Users/dschmidt/tmp/t.php:13

不确定它达到了什么限制,但composer update达到了类似但不同的下限:

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)

Fatal error: Out of memory (allocated 57671680) (tried to allocate 2110325 bytes) in phar:///cygdrive/c/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Util/RemoteFilesystem.php on line 462

标签: php

解决方案


原来我做了另一个 Cygwin 更新,它更新了一堆 php 模块。这解决了我的问题。大约 10 天前我完成了 Cygwin 更新,所以你一定是 Cygwin php 模块的那个小窗口中的一个错误。

很高兴能解决这个问题。对不起所有花时间为我考虑的人。


推荐阅读