Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Memory management issue - Creating and destroying a token stream 1000 times increases the memory usage with 736mb when running in HHVM cli #60

Closed
motin opened this issue Jun 6, 2016 · 4 comments

Comments

@motin
Copy link

motin commented Jun 6, 2016

I found a memory leak in phpunit's php-token-stream library used in code coverage calculations.

Using php-token-stream v1.4.8 in hhvm 3.12.0

The following test case that shows the issue (I copied Token.php into the tests/_fixture directory before running the test):

class PHP_Token_MemoryManagementTest extends PHPUnit_Framework_TestCase
{

    public function testMemoryLeak()
    {
        $initialMemoryUsage = memory_get_usage(true);
        for ($i=0; $i<1000; $i++) {
            $this->createAndDestroyTokenStream();
        }
        $additionalMemoryUsage = memory_get_usage(true) - $initialMemoryUsage;
        $this->assertLessThan(10, round($additionalMemoryUsage / 1024 / 1024, 2), 'Creating and destroying a token stream for Token.php 1000 times increases the memory usage with less than 10mb');
    }

    protected function createAndDestroyTokenStream()
    {
        $tokenStream = new PHP_Token_Stream(
          TEST_FILES_PATH . 'Token.php'
        );
        unset($tokenStream);
    }

}

Result:

There was 1 failure:

1) PHP_Token_MemoryManagementTest::testMemoryLeak
Creating and destroying a token stream for Token.php 1000 times increases the memory usage with less than 10mb
Failed asserting that 736.27 is less than 10.

What this says is that calculating code coverage requires 700mb of memory per 1000 files as large as Token.php (around 600 lines) in the project, practically making the code coverage reports useless for larger projects or where the vendor directory is whitelisted.

@sebastianbergmann
Copy link
Owner

  • This may be besides the point of the issue but why would you whitelist the vendor directory?
  • Have you tried enabling this component's token cache using PHPUnit's cacheTokens="true" configuration option?

@motin
Copy link
Author

motin commented Jun 6, 2016

@sebastianbergmann

This may be besides the point of the issue but why would you whitelist the vendor directory?

We do that sometimes to check how much of our dependencies are actually used, so that we can prune those that we barely touch.

Have you tried enabling this component's token cache using PHPUnit's cacheTokens="true" configuration option?

Yes, that would solve the memory issue when re-scanning the same source files over and over, but not affect the case when there are for instance 1000 different source files to be scanned.

motin added a commit to motin/php-token-stream that referenced this issue Jun 6, 2016
motin added a commit to motin/php-token-stream that referenced this issue Jun 6, 2016
@motin
Copy link
Author

motin commented Jun 7, 2016

This is not reproduced in PHP 5.6 - running the same test with 1000 executions finishes in just about 9.5mb additional memory usage!
Feel free to close this issue if it is not supposed to be work with HHVM.

@motin motin changed the title Memory management issue - Creating and destroying a token stream 1000 times increases the memory usage with 736mb Memory management issue - Creating and destroying a token stream 1000 times increases the memory usage with 736mb when running in HHVM cli Jun 7, 2016
@sebastianbergmann
Copy link
Owner

Looks like an HHVM issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants