php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75511 fread not free unused buffer.
Submitted: 2017-11-10 08:53 UTC Modified: 2017-11-13 01:10 UTC
From: mix5003 at gmail dot com Assigned:
Status: Closed Package: Performance problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
 [2017-11-10 08:53 UTC] mix5003 at gmail dot com
Description:
------------
When use fread to read multiple small files with large buffer. 
it may make a memory limit error.

i think it becuase php alloc memory equals length that i send to fread and it not trim to actual size of string. so many small string can reach a memory limit.

or is it normal behavior?

Test script:
---------------
<?php
ini_set('memory_limit', '256M');
// create file
@mkdir('tmp_file');
for($i=0;$i<300;$i++){
file_put_contents("tmp_file/{$i}.txt", $i);
}
// read file
$data = [];
for($i=0;$i<300;$i++){
$fp = fopen("tmp_file/{$i}.txt", "r");
$data[$i] = fread($fp, 1024 * 1024);
fclose($fp);
echo strlen($data[$i])." ".memory_get_usage()."\n";
}

Expected result:
----------------
it can run normally. no memory allocate error.

Actual result:
--------------
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 1048577 bytes)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-13 01:10 UTC] rasmus@php.net
-Status: Open +Status: Verified
 [2017-11-13 01:10 UTC] rasmus@php.net
Yes, you are correct. The fread() code doesn't re-allocate the string to just the length of the read bytes (plus \0). It obviously should.
 [2017-11-13 03:14 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=1c1df0d9370fa5a14644850f09a7f9377bfc83be
Log: Fixed bug #75511 (fread not free unused buffer)
 [2017-11-13 03:14 UTC] laruence@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC