Bug #75511 [Opn->Ver]: fread not free unused buffer.
| From: | rasmus@php.net | Date: | Mon, 13 Nov 2017 01:10:03 +0000 |
| Subject: | Bug #75511 [Opn->Ver]: fread not free unused buffer. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212554@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75511&edit=1
ID: 75511
Updated by: rasmus@php.net
Reported by: mix5003 at gmail dot com
Summary: fread not free unused buffer.
-Status: Open
+Status: Verified
Type: Bug
Package: Performance problem
Operating System: *
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2017-11-10 08:53:50] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75511&edit=1