php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51523 Memory leak on fread()
Submitted: 2010-04-10 01:06 UTC Modified: 2010-04-10 20:24 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: evilzluk at gmail dot com Assigned:
Status: Closed Package: Performance problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2010-04-10 01:06 UTC] evilzluk at gmail dot com
Description:
------------
The problem is the fread() uses a normal amount of a memory. But there are too many unallocated anonymous memory pages.
So if the file size >2G the script may cause eating up to 2G of RAM. But the script's runtime memory is 5M.

The problem is occured even if:
$fp = fopen($file, "rb");
while(!feof($fp))
    fread($fp, 1024);
fclose($fp);

After that the memory isn't released so we have a garbadge in the memory.


Test script:
---------------
<?php

$file = "A.very.big.file.avi";
ob_start();
$fp = fopen($file, "wb");
if ($fp)
{
    while(!feof($fp))
    {
        echo(fread($fp, 1024));
        if (ob_get_length())
        {
            ob_flush();
            flush();
            ob_end_flush();
        }
    }
    fclose($fp);

    @ob_flush();
    @flush();
    @ob_end_flush();
    @ob_end_clean();
}
?>


Expected result:
----------------
The total amount of a memory usage should be at least <php script runtime memory usage> + 1024 (+ some buffer (up to 8192)). But not almost all the physical memory (0...unlimited)




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-10 17:05 UTC] evilzluk at gmail dot com
Description:
------------
The problem is the fread() uses a normal amount of a memory. But there are too many unallocated anonymous memory pages.
So if the file size >2G the script may cause eating up to 2G of RAM. But the script's runtime memory is 5M.

The problem is occured even if:
$fp = fopen($file, "rb");
while(!feof($fp))
    fread($fp, 1024);
fclose($fp);

After that the memory isn't released so we have a garbadge in the memory.


Test script:
---------------
<?php

$file = "A.very.big.file.avi";
ob_start();
$fp = fopen($file, "rb");
if ($fp)
{
    while(!feof($fp))
    {
        echo(fread($fp, 1024));
        if (ob_get_length())
        {
            ob_flush();
            flush();
            ob_end_flush();
        }
    }
    fclose($fp);
}
@ob_flush();
@flush();
@ob_end_flush();
@ob_end_clean();

?>


Expected result:
----------------
The total amount of a memory usage should be at least <php script runtime memory usage> + 1024 (+ some buffer (up to 8192)). But not almost all the physical memory (0...unlimited)
 [2010-04-10 20:24 UTC] evilzluk at gmail dot com
-Status: Open +Status: Closed
 [2010-04-10 20:24 UTC] evilzluk at gmail dot com
resolved adding to cronjob:
sync
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
sync
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Mar 31 08:01:26 2020 UTC