-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak when using Http facade #51109
Comments
Its worth noting that this is particularly problematic for long running processes like workers. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
If you could create a minimal reproduction for this and we take a closer look?
|
Hmm! I was able to reproduce this on a completly fresh copy of laravel on version 11 also. I create a clean clone ( |
I just did a bit more testing, it looks like the memory leak kinda starts randomly, in my case around the 185th iteration. for($i=1; $i < 10000; $i++) {
Http::get('https://google.com');
gc_collect_cycles();
dump("loop $i | " . memory_get_peak_usage() / 1000000 . 'MB');
} Sample output:
When I use |
Okay, I can see this kick into gear around ~218th loop (using a while loop). |
On further investigation:
Update to report actual memory used and reporting via <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class HttpTest extends Command
{
protected $signature = 'app:http-test';
public function handle()
{
while (true) {
Http::get('https://tim.macdonald.au/assets/css/style.css?id=aefb07c1238deb82d60c')->throw();
gc_collect_cycles();
echo memory_get_usage() / 1_000_000 . 'MB'.PHP_EOL;
}
}
} After the first report, every subsequent echo reported exactly the same value with 1,200+ invocations. I'm also seeing that Looks like it might be something to do with the underlying dumper. |
Huh, yah there seems to be a couple areas where something is leaking. I have updated my test to look like this: class ExampleTest extends TestCase
{
public function testHttpPost()
{
Http::fake(['bythepixel.com' => Http::response(['ok' => 'ok'])]);
$startMemoryUsage = memory_get_usage();
$startMemoryPeakUsage = memory_get_peak_usage();
for($i=1; $i < 10000; $i++) {
Http::get('https://bythepixel.com');
gc_collect_cycles();
}
$endMemoryUsage = memory_get_usage();
$endMemoryPeakUsage = memory_get_peak_usage();
echo "\nStart memory usage: {$this->formatMB($startMemoryUsage)}";
echo "\nStart memory peak usage: {$this->formatMB($startMemoryPeakUsage)}";
echo "\nEnd memory usage: {$this->formatMB($endMemoryUsage)}";
echo "\nEnd memory peak usage: {$this->formatMB($endMemoryPeakUsage)}";
}
private function formatMB($number): string
{
return $number / 1000000 . 'MB';
}
} I ran test with Http::fake() and without. Here are my results:
My conclusion from this little experiment is that Thanks for helping me look into this! |
No worries. Was kinda fun digging into this one. |
Maybe you need to do it manually close the stream and any underlying resources: framework/src/Illuminate/Http/Client/Response.php Lines 243 to 254 in 980e8d5
before: for($i=1; $i < 1000; $i++) {
Http::get('https://google.com');
dump(memory_get_peak_usage() / 1000000 . 'MB');
} after: $response = Http::get('https://google.com');
// do something...
$response->close();
dump(memory_get_peak_usage() / 1000000 . 'MB'); |
Laravel Version
10.48.8
PHP Version
8.3.4
Database Driver & Version
No response
Description
When using the Http facade to make http requests, there appears to be a memory leak. I wrote a simple phpunit test on a clean Laravel install (10.48.8) like this:
In the terminal I see:
I have scaled this up and memory just slowly rises, there does not seem to be a top off point (until you run out of memory with php).
I have also tried adding manual
gc_collect_cycles()
calls just in case without any help.Steps To Reproduce
Write a unit test with:
Observe slow memory creep.
The text was updated successfully, but these errors were encountered: