|  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
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:
ini_set('memory_limit', '256M');
// create file
file_put_contents("tmp_file/{$i}.txt", $i);
// read file
$data = [];
$fp = fopen("tmp_file/{$i}.txt", "r");
$data[$i] = fread($fp, 1024 * 1024);
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)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-13 01:10 UTC]
-Status: Open +Status: Verified
 [2017-11-13 01:10 UTC]
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]
Automatic comment on behalf of
Log: Fixed bug #75511 (fread not free unused buffer)
 [2017-11-13 03:14 UTC]
-Status: Verified +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 03:01:28 2024 UTC