PHP X86内存限制



我有一个我需要用于418 MB的base64编码需要的文件,但我不断获得内存限制错误。我已经在我的php.ini中设置了memory_limit=2048M,但我仍然无法将其读取为带有X86 CLI版本的php的base64字符串。

php脚本

<?php
echo "rn";
echo "rnMemory Usagern" . memory_get_usage(false) . " BytesrnReal " . memory_get_usage(true) . " Bytesrn";
echo "rnPeak Memory Usagern" . memory_get_peak_usage(false). " BytesrnReal " . memory_get_peak_usage(true) . " Bytesrn";
echo "rnMemory Limitrn" . ini_get('memory_limit') . "rn";
$file = file_get_contents('C:PathToFile');
echo "rn";
echo "rnMemory Usagern" . memory_get_usage(false) . " BytesrnReal " . memory_get_usage(true) . " Bytesrn";
echo "rnPeak Memory Usagern" . memory_get_peak_usage(false). " BytesrnReal " . memory_get_peak_usage(true) . " Bytesrn";
echo "rnMemory Limitrn" . ini_get('memory_limit') . "rn";
$encodedFile = base64_encode($file);
echo "rn";
echo "rnMemory Usagern" . memory_get_usage(false) . " BytesrnReal " . memory_get_usage(true) . " Bytesrn";
echo "rnPeak Memory Usagern" . memory_get_peak_usage(false). " BytesrnReal " . memory_get_peak_usage(true) . " Bytesrn";
echo "rnMemory Limitrn" . ini_get('memory_limit') . "rn";
?>

运行该脚本会产生以下内容。

PS C:> php .MemoryIssueTest.php

Memory Usage
382184 Bytes
Real 2097152 Bytes
Peak Memory Usage
422256 Bytes
Real 2097152 Bytes
Memory Limit
2048M

Memory Usage
447075680 Bytes
Real 448790528 Bytes
Peak Memory Usage
447084280 Bytes
Real 448790528 Bytes
Memory Limit
2048M
VirtualAlloc() failed: [0x00000008] Not enough memory resources are available to process this command.

VirtualAlloc() failed: [0x00000008] Not enough memory resources are available to process this command.
PHP Fatal error:  Out of memory (allocated 448790528) (tried to allocate 593015064 bytes) in C:MemoryIssueTest.php on line 14
PHP Stack trace:
PHP   1. {main}() C:MemoryIssueTest.php:0
PHP   2. base64_encode() C:MemoryIssueTest.php:14
Fatal error: Out of memory (allocated 448790528) (tried to allocate 593015064 bytes) in C:MemoryIssueTest.php on line 14
Call Stack:
    0.4046     382152   1. {main}() C:MemoryIssueTest.php:0
   33.4669  447075680   2. base64_encode() C:MemoryIssueTest.php:14

升级到64位PHP后,我从同一脚本中获得以下结果。

PS C:> php .MemoryIssueTest.php

Memory Usage
402088 Bytes
Real 2097152 Bytes
Peak Memory Usage
444160 Bytes
Real 2097152 Bytes
Memory Limit
2048M

Memory Usage
440804144 Bytes
Real 442499072 Bytes
Peak Memory Usage
440812824 Bytes
Real 442499072 Bytes
Memory Limit
2048M

Memory Usage
1025909576 Bytes
Real 1027604480 Bytes
Peak Memory Usage
1025909760 Bytes
Real 1027604480 Bytes
Memory Limit
2048M

从我的理解中,X86应用程序应该能够比1GB多消耗的内存。X86版本的PHP硬编码是否仅允许少于1GB?如果没有,当我只需要消耗1GB而又有2GB限制时,为什么我会得到PHP Fatal error: Out of memory错误?

从理论上讲,您应该能够在Windows上使用X86进程分配多达2GB的内存。但是,关于您可以分配多少的其他限制。至关重要的:可用地址空间中的内存碎片。由于base64_encode一次一次分配所需的内存,因此您需要足够的连续地址空间来编码整个字符串。您更有可能拥有64位地址空间,这就是为什么PHP的64位版本正常运行的原因。

有关此答案中的更多信息:Java Windows XP上的最大内存

附录:在我的测试中,我只能分配比您的分配略多,但是较小的地址分配让我更接近2GB限制。

附录2:如果您绝对需要使用32位版本并编码为文件,则可以通过在3个字节中的某些倍数中编码来解决此问题。

// Example, from memory string to file.
$current_position = 0;
$length = strlen($file);
$outputfile = fopen('testoutput.txt', 'w');
while($current_position < $length) {
  fwrite($outputfile, base64_encode(substr($file, $current_position, 60000)));
  $current_position += 60000;
}
fclose($outputfile);

或从文件到文件一直到一路,这几乎无法分配内存:

$inputfile = fopen('test.mp4', 'rb');
$outputfile = fopen('testoutput2.txt', 'w');
while( ($str = fread($inputfile, 61440)) !== false ) {
  if($str === '') {
    break;
  }
  fwrite($outputfile, base64_encode($str));
  $current_position += 61440;
}
fclose($outputfile);

检查您是否有足够的内存。如果您只有&lt; 1GB免费系统内存(或其他虚拟内存限制(例如容器内存限制))

,则更改限制并更改可寻址内存的数量(64位)

最新更新