gzinflate:最大空间以避免内存不足?

时间:2022-03-08 22:19:41

When decompressing with gzinflate, I found that - under certain circumstances - the following code results in out-of-memory errors. Tested with PHP 5.3.20 on an 32bit Linux (Amazon Linux AMI on EC2).

使用gzinflate解压缩时,我发现 - 在某些情况下 - 以下代码会导致内存不足错误。在32位Linux(EC2上的Amazon Linux AMI)上使用PHP 5.3.20进行测试。

$memoryLimit = Misc::bytesFromShorthand(ini_get('memory_limit')); // 256MB
$memoryUsage = memory_get_usage(); // 2MB in actual test case
$remaining = $memoryLimit - $memoryUsage;
$factor = 0.9;
$maxUncompressedSize = max(1, floor($factor * $remaining) - 1000);
$uncompressedData = gzinflate($compressedData, $maxUncompressedSize);

Although, I calculated the size of $maxUncompressedSize conservatively, hoping to give gzinflate sufficient memory, I still get:

虽然,我保守地计算了$ maxUncompressedSize的大小,希望给gzinflate足够的内存,我仍然得到:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 266143484 bytes) in foo.php on line 123

致命错误:第123行的foo.php中允许的内存大小为268435456个字节(尝试分配266143484个字节)

When changing the value of $factor from 0.9 to 0.4, then the error goes away, in this case. In other cases 0.9 is OK.

将$ factor的值从0.9更改为0.4时,在这种情况下错误消失。在其他情况下0.9是可以​​的。

I wonder:

Is the reason for the error really that gzinflate needs more than double the space of uncompressed data? Is there possibly some other reason? Is $remaining really the remaining memory at disposal to the application?

错误的原因是否真的需要gzinflate需要超过未压缩数据空间的两倍?可能还有其他原因吗? $剩余的确实是应用程序可用的剩余内存吗?

1 个解决方案

#1


1  

It is indeed possible. IMHO, the issue lies with memory_get_usage(true).

这确实是可能的。恕我直言,问题在于memory_get_usage(true)。

Using true should give a higher memory usage value, because should take everything into account.

使用true应该提供更高的内存使用价值,因为应该考虑所有因素。

#1


1  

It is indeed possible. IMHO, the issue lies with memory_get_usage(true).

这确实是可能的。恕我直言,问题在于memory_get_usage(true)。

Using true should give a higher memory usage value, because should take everything into account.

使用true应该提供更高的内存使用价值,因为应该考虑所有因素。