From: mgr at woodwing dot com
Operating system: Windows / OS X
PHP version: 5.2.9
PHP Bug Type: Performance problem
Bug description: file_get_contents and fread use twice the memory necessary
Description:
------------
When using file_get_contents or fread to read a file completely in memory,
PHP allocates twice the filesize memory.
PHP versions 5.2.5 and 5.2.6 don't have this problem but 5.2.8 and 5.2.9
do.
We're working a lot with large files and this problem affects the memory
usage (out of memory) and the performance (longer duration).
Are there any alternatives to work-around this problem besides not reading
the complete file in memory?
Reproduce code:
---------------
$filePath = tempnam(sys_get_temp_dir(), '');
$data = str_pad('', 1024);
if (($fh = fopen($filePath, 'wb'))){
for ($i=0; $i < 4096; $i++){
fputs($fh, $data);
}
fclose($fh);
$filesize = filesize($filePath);
print "filesize = " . number_format($filesize) . "\n";
print "Memory usage at start\n";
print "mem = " . number_format(memory_get_usage()) . "; peak = " .
number_format(memory_get_peak_usage()) . "\n";
$x = file_get_contents($filePath);
print "\nAfter\n";
print "mem = " . number_format(memory_get_usage()) . "; peak = " .
number_format(memory_get_peak_usage()) . "\n";
unlink($filePath);
}
Expected result:
----------------
PHP allocates once the filesize memory.
filesize = 4,194,304
Memory usage at start
mem = 66,992; peak = 90,228
After
mem = 4,261,396; peak = 4,269,740
Actual result:
--------------
filesize = 4,194,304
Memory usage at start
mem = 71,300; peak = 96,044
After
mem = 4,265,704; peak = 8,460,160
--
Edit bug report at http://bugs.php.net/?id=48152&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=48152&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=48152&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=48152&r=trysnapshot60
Fixed in CVS:
http://bugs.php.net/fix.php?id=48152&r=fixedcvs
Fixed in CVS and need be documented:
http://bugs.php.net/fix.php?id=48152&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=48152&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=48152&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=48152&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=48152&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=48152&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=48152&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=48152&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=48152&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=48152&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48152&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=48152&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=48152&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=48152&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=48152&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=48152&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=48152&r=mysqlcfg