Description
- Laravel Version: 9.30.1
- PHP Version: 8.1.10
- Database Driver & Version: N/A
Description:
We've been having memory related issues in CI recently where the memory consumption continuously grows. This is a particular problem when enabling coverage checks (i.e. artisan test --coverage
) as this eventually exhausts the memory in the CI environment. A few weeks ago we thought we had this traced back to this issue in PHP core which was fixed as of v8.1.10, however, after updating to 8.1.10 we're still having memory issues in CI.
I recently found this article which recommends creating a test to repeatedly create, boot and flush the (Laravel) application multiple times to see the memory consumption over time. I did this on a fresh Laravel application and, as you can see below, the memory usage constantly rises over time.
I tried to reproduce this on a personal project that doesn't use Laravel to determine if it was an issue with phpunit or PHP core but was unable to reproduce the leak so I'm leaning towards Laravel at the moment (however, I'm not convinced of this yet).
I haven't dug any deeper at this point as I wanted to bring this to your attention but am happy to keep investigating if necessary.
Steps To Reproduce:
Create a new Laravel application.
$ laravel new test-app
Add the following test case to the newly created application.
public function test_leaks_memory_on_1000_iterations()
{
$this->app->flush();
$this->app = null;
for ($i = 1; $i < 1000; ++$i) {
$this->createApplication()->flush();
dump('Using ' . ((int) (memory_get_usage(true) / (1024 * 1024))) . 'MB in ' . $i . ' iterations.');
}
$this->app = $this->createApplication();
}
Run the test and observe the ever increasing memory consumption.
$ phpunit tests/Feature/ExampleTest.php
PHPUnit 9.5.24 #StandWithUkraine
^ "Using 20MB in 1 iterations."
^ "Using 20MB in 2 iterations."
^ "Using 20MB in 3 iterations."
[TRUNCATED FOR BREVITY]
^ "Using 26MB in 100 iterations."
[TRUNCATED FOR BREVITY]
^ "Using 28MB in 200 iterations."
[TRUNCATED FOR BREVITY]
^ "Using 38MB in 500 iterations."
[TRUNCATED FOR BREVITY]
^ "Using 48MB in 800 iterations."
[TRUNCATED FOR BREVITY]
^ "Using 54MB in 997 iterations."
^ "Using 54MB in 998 iterations."
^ "Using 54MB in 999 iterations."
The more iterations, the more memory used.