Bug #75511 [Ver->Csd]: fread not free unused buffer.

From: Date: Mon, 13 Nov 2017 03:14:29 +0000
Subject: Bug #75511 [Ver->Csd]: fread not free unused buffer.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-212556@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: laruence@php.net Reported by: mix5003 at gmail dot com Summary: fread not free unused buffer. -Status: Verified +Status: Closed Type: Bug Package: Performance problem Operating System: * PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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) Previous Comments: ------------------------------------------------------------------------ [2017-11-13 01:10:00] 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-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

« previous php.bugs (#212556) next »