0

When I run PHPUnit, it appears to me as if it had a memory-leak when running many tests inside a single test class. But I don't know if this is a bug or it was the expected behaviour.

To reproduce:

  • I create a simple testHello() with a silly assertTrue(true).
  • I feed it from providerHello(). Just feeding 3 dummy params.
  • With $numberOfTests = 1;, consumed memory is 5.75MB.
    • PHPUnit output = Time: 0 seconds, Memory: 5.75Mb
  • With $numberOfTests = 10000;, I don't expect the memory to grow so much, just the size of the new array. But the used memory is 99.75MB which I feel it is too much.
    • PHPUnit output = Time: 4 seconds, Memory: 99.75Mb
  • I added a dirty echo() in the provider, just to know how much memory the array made the script to consume.
    • With 1 test: Memory = 5294552 (5.2MB)
    • With 10.000 tests: Memory = 15735352 (15.7MB)

The questions:

  • Why do I loose 84MB in the way? (99.75 really consumed - 15.75 really used by the array)
  • Is it hormal that it allocates memory at each iteration, probably its internal setUp(), but does not free the same amount at the internal tearDown()?
  • Am I doing anything wrong?

My version:

phpunit --version gives PHPUnit 3.6.10 by Sebastian Bergmann..

This is the code:

<?php
class DemoTest extends \PHPUnit_Framework_TestCase
{
    /** @dataProvider providerHello */
    public function testHello( $a, $b, $c )
    {
        $this->assertTrue( true );
    }

    public function providerHello()
    {
        $numberOfTests = 10000;

        $data = array();

        for( $i = 0; $i < $numberOfTests; $i++ )
        {
            $data[] = array( 1, 2, 3 );
        }

        echo( "Memory = " . memory_get_peak_usage() . PHP_EOL );

        return $data;
    }

}
?>
Xavi Montero
  • 9,239
  • 7
  • 57
  • 79

1 Answers1

0

you need to set backupGlobals and backupStaticAttributes to false in your phpunit.xml file. If you don't use an config file you can also do so on the command line.

 --no-globals-backup
 --static-backup
Josh Woodcock
  • 2,683
  • 1
  • 22
  • 29