hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Memory Leak in SplStack

Open schmittjoh opened this issue 11 years ago • 5 comments

There seems to be a memory leak somewhere in the SplStack implementation. This can be observed when running the following script:

<?php

$stack = new \SplStack();
$stack->push(new Snapshot(Snapshot::FIRST_STEP, array()));
//$stack = array();
//$stack[] = new Snapshot(Snapshot::FIRST_STEP, array());

$i = 0;

nextIteration: if ( ! $stack->isEmpty() /*empty($stack)*/) {
    $i++;

    if ($i % 10000 === 0) {
        var_dump('Iterations: '.$i.' - Stack Size: '.count($stack));
    }

    $snapshot = $stack->pop();
//    $snapshot = array_pop($stack);

    switch ($snapshot->stage) {
        case Snapshot::THIRD_STEP:
            goto nextIteration;

        case Snapshot::SECOND_STEP:
            for ($k=0; $k<10000; $k++) {
                $stack->push(new Snapshot(Snapshot::THIRD_STEP, array('fidjfidf', 'difjdifjd')));
//                $stack[] = new Snapshot(Snapshot::THIRD_STEP, array('fidjfidf', 'difjdifjd'));
            }
            goto nextIteration;

        case Snapshot::FIRST_STEP:
            for ($k=0; $k<10000; $k++) {
                $stack->push(new Snapshot(Snapshot::SECOND_STEP, array('foo', 'bar')));
//                $stack[] = new Snapshot(Snapshot::SECOND_STEP, array('foo', 'bar'));
            }
            goto nextIteration;
    }
}


class Snapshot
{
    const FIRST_STEP = 0;
    const SECOND_STEP = 1;
    const THIRD_STEP = 2;

    public $stage;
    public $result;

    public function __construct($stage, $result) 
    {
        $this->stage = $stage;
        $this->result = $result;
    }
}

When using arrays, there is no memory leak. When using the SplStack structure, memory consumption increases very quickly.

Just if you are wondering what this script does, it is the skeleton of converting a recursive algorithm to an iterative one (to avoid stack overflow issues).

HHVM Version:

HipHop VM 3.7.0 (rel)
Compiler: tags/HHVM-3.7.0-0-gc8baf9cd3cb603e030969bfe24634d5e85549915
Repo schema: 1988ce75e6a571ce9c79c60fd7dc3ca8a5a5d403

schmittjoh avatar May 24 '15 10:05 schmittjoh

Thanks for the report, and especially for the repro case. Someone will take a look when we get a chance.

jwatzman avatar May 24 '15 13:05 jwatzman

Hi @schmittjoh, what tool do you use to measure memory? Could you please provide sample output comparing PHP and HHVM?

aingram avatar May 25 '15 06:05 aingram

I was looking at htop in another terminal; with arrays the consumption was steady at around 600 MB while the SplStack version was constantly increasing - I terminated the script when it was consuming more than 10 GB of memory.

Haven't tried with vanilla PHP, any reason you are interested in that?

schmittjoh avatar May 25 '15 12:05 schmittjoh

I believe the following code is a smaller reproduction of the core problem. I confirmed the problem doesn't happen in any version of PHP (http://3v4l.org/7cB7B):

$stack = new \SplStack();
echo memory_get_usage(true) . "\n";
for ($i = 0; $i < 100000; ++$i) {
  if ($i % 1000 === 0) {
    echo memory_get_usage(true) . "\n";
  }
  $stack->push('foo');
  $stack->push('foo');
  $stack->pop();
  $stack->pop();
}

aingram avatar May 25 '15 14:05 aingram

The way the stack is implemented (using a doubly linked list) creates reference cycles. It looks as though push() and pop() don't break these cycles when removing elements (they only update the internally tracked head and tail references). Since hhvm lacks a cyclic garbage collector this leads to live objects leaking from the stack.

It should be pretty easy to update pop() and shift() to update the internal list pointers (there are probably other bugs that would also be fixed by doing this).

https://github.com/facebook/hhvm/blob/master/hphp/system/php/spl/datastructures/SplDoublyLinkedList.php#L143-L148

paulbiss avatar May 25 '15 18:05 paulbiss