php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71297 Memory leak with yield from
Submitted: 2016-01-07 02:13 UTC Modified: -
From: karl dot malakoff at dataprocessors dot com dot au Assigned:
Status: Closed Package: Reproducible crash
PHP Version: 7.0.1 OS: Windows 7
Private report: No CVE-ID: None
 [2016-01-07 02:13 UTC] karl dot malakoff at dataprocessors dot com dot au
Description:
------------
I've found a memory leak that occurs when iterating over a generator that directly yields from another generator inside a loop (see genLeak()).

It seems that this can be worked around by assigning the result of the yield from to a temp var and then yielding that var (see genNoLeak()).

Directly yielding is also fine (see gen()).

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

function foo()
{
    yield array_fill(0, 10000, 4);
}

function gen()
{
    while(true){
        yield array_fill(0, 10000, 4);
        print memory_get_usage(false).PHP_EOL;
    }
}

function genLeak()
{
    while(true){
        yield from foo();
        print memory_get_usage(false).PHP_EOL;
    }
}

function genNoLeak()
{
    while(true){
        $tmp = yield from foo();
        yield $tmp;
        print memory_get_usage(false).PHP_EOL;
    }
}

foreach(genLeak() as $i){}


Expected result:
----------------
Memory usage remains constant though each iteration of the loop.

Actual result:
--------------
Memory usage rises until php runs out of memory and crashes. 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-07 10:56 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=033d6087711332efab4fbb1cb90acabd9af8534b
Log: Fixed bug #71297 (Memory leak with yield from)
 [2016-01-07 10:56 UTC] bwoebi@php.net
-Status: Open +Status: Closed
 [2016-01-07 10:57 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=033d6087711332efab4fbb1cb90acabd9af8534b
Log: Fixed bug #71297 (Memory leak with yield from)
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=033d6087711332efab4fbb1cb90acabd9af8534b
Log: Fixed bug #71297 (Memory leak with yield from)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC