Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP x86内存限制_Php_Memory_X86_64 Bit - Fatal编程技术网

PHP x86内存限制

PHP x86内存限制,php,memory,x86,64-bit,Php,Memory,X86,64 Bit,我有一个文件,我需要base64编码,这是418MB,但我不断得到内存限制错误。我在php.ini中设置了memory\u limit=2048M,但在x86 CLI版本的php中,我仍然无法将其作为base64字符串读入内存 PHP脚本 运行该脚本会产生以下结果。 PS C:\> php .\MemoryIssueTest.php Memory Usage 382184 Bytes Real 2097152 Bytes Peak Memory Usage 422256 Byte

我有一个文件,我需要base64编码,这是418MB,但我不断得到内存限制错误。我在php.ini中设置了
memory\u limit=2048M
,但在x86 CLI版本的php中,我仍然无法将其作为base64字符串读入内存

PHP脚本


运行该脚本会产生以下结果。

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
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
升级到64位版本的PHP后,我从相同的脚本中得到以下结果。

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
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的内存?如果没有,为什么会出现
PHP致命错误:内存不足
错误,而我只需要消耗1GB,但有2GB的限制?

理论上,在Windows上使用x86进程应该可以分配最多2GB的内存。但是,对于您可以分配的数量还有其他限制。关键:可用地址空间中的内存碎片。由于
base64\u encode
一次分配所需内存,因此需要足够的连续地址空间来编码整个字符串。使用64位地址空间,您可能会有足够的空间,这就是为什么64位版本的PHP运行良好的原因

有关此答案中的更多信息:

附录:在我的测试中,我只能分配比你略多的地址,但更小的地址分配让我更接近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);

检查你是否有足够的内存。更改限制和可寻址内存量(64位)如果你将
内存限制设置为
-1
,让x86版本可以正常运行,这无关紧要吗?一旦你对文件进行了base64编码,你将如何处理它?@StephanieTemple将其发送到Elasticsearch进行索引。我的机器有16GB的RAM,根据resmon的说法,它只使用了大约3GB在测试时对其进行测试。