我有一个需要大量内存的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的官方限制吗?
我相信32位进程的理论极限是3GB(当然也有技巧),但PHP在崩溃前很难达到1GB,至少根据我的经验。
您至少应该尝试优化您的代码,因为一开始您正在内存中加载一组ZIP档案,可能没有充分的理由这样做。