Gzinflate: max. space to avoid running out of memory?

When unpacking with gzinflate I found that - under certain circumstances - the following code leads to out-of-memory errors. Tested with PHP 5.3.20 on 32-bit Linux ( Amazon Linux AMI on EC2 ).

 $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 $maxUncompressedSize size conservatively, hoping to give enough gzinflate memory, I still get:

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

If you change the value of $factor from 0.9 to 0.4 , then the error will disappear, in this case. In other cases, 0.9 is fine.

Interesting:

Is the cause of the error really that gzinflate needs to more than double the space of uncompressed data? Perhaps there is another reason? Is the $remaining remaining remaining memory available to the application?

+4
source share
1 answer

It is really possible. IMHO, the problem is memory_get_usage(true) .

Using true should give a higher value for memory usage, since it should take everything into account.

+1
source

All Articles