我有一个需要大量内存的PHP CLI应用程序 . 我在php.ini中将 memory_limit 设置为-1 . 不幸的是我仍然遇到这个问题:
致命错误:内存不足(分配870318080)(试图分配138412032字节)
这时我的系统有超过4 GB的可用内存(我安装了12 GB) . 所以这不是硬件限制 .
我也尝试在代码中设置它:
ini_set('memory_limit','-1');
?>
我也做的是将它设置为2048M,但错误仍然存在 . 它总是大约1GB,那么有任何硬编码的1GB限制吗?
我的系统是什么样的:
Windows 7
PHP 5.6.20(没有Suhosin且没有safe_mode的线程安全32位)
更新:
以下是重现它的脚本:
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中的官方限制吗?