【发布时间】:2016-08-13 04:01:39
【问题描述】:
我有一个需要大量内存的 PHP CLI 应用程序。我在 php.ini 中将 memory_limit 设置为 -1。不幸的是,我仍然遇到这个问题:
致命错误:内存不足(分配870318080)(试图分配138412032字节)
此时我的系统有超过 4 GB 的可用内存(我安装了 12 GB)。所以这不是硬件限制。
我也尝试在代码中设置:
<?php
ini_set('memory_limit','-1');
?>
我也将其设置为 2048M,但错误仍然存在。它总是在 1GB 左右,所以有硬编码的 1GB 限制吗?
我的系统是什么样子的:
- Windows 7
- PHP 5.6.20(不带 Suhosin 且不带 safe_mode 的线程安全 32 位)
更新:
这是重现它的脚本:
<?php
ini_set("memory_limit", -1);
echo formatSizeUnits(memory_get_usage()).PHP_EOL;
for($i=0;$i<=10; $i++) {
$content[$i] = file_get_contents("http://mirror.softaculous.com/apache/lucene/solr/6.0.0/solr-6.0.0.zip");
echo formatSizeUnits(memory_get_usage()).PHP_EOL;
}
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
?>
我的输出:
php test.php
123.37 KB
164.25 MB
328.37 MB
492.49 MB
PHP Fatal error: Out of memory (allocated 688914432) (tried to allocate 171966464 bytes) in test.php on line 12
Fatal error: Out of memory (allocated 688914432) (tried to allocate 171966464 bytes) in test.php on line 12
更新 2:
我安装了 x64 PHP 5.6.20。现在我能够克服这个限制。这是 x86 PHP 的官方限制吗?
【问题讨论】:
-
您的 Windows 操作系统是 32 位的?您需要显示一些可能导致内存泄漏的实际代码。失控循环?谁知道。没有代码,我帮不了你。
-
没有 Windows 是 64 位的。
-
@Marcus 我为你添加了测试代码
标签: php memory memory-management