-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[5.8] Memory leak in DB join queries #28195
Comments
p.s. and the size of the memory leak is related to the size of the join dataset. So in the example above it looks small, but when you have large data sets, it causes the memory to rapidly get out of hand. |
I can reproduce this, but the memory usage regularly "resets" (when the garbage collection kicks in?):
Do you get the same behavior? |
Interesting... yes I do. If I add a counter, and check when the reset occurs, its the same at 1423 loops (at least for me). The problem is for my "real" script - it only lasts 20-25 loops before the memory failure kicks in (due to the size of the queries). If I add a So what's the root cause though? I'm guessing a reference not being released somewhere... |
The memory leak is caused by this line:
Every The question is whether this is the expected behavior or a PHP bug. |
Simple example: class A
{
public $b;
public function b()
{
$this->b = new B($this);
}
}
class B
{
public $a;
public function __construct($a)
{
$this->a = $a;
}
}
while (true) {
(new A)->b();
var_dump(memory_get_usage());
usleep(1000);
} |
This seems relevant: https://medium.com/@johann.pardanaud/about-circular-references-in-php-10f71f811e9 According to that, a solution is coming in PHP7.4: https://wiki.php.net/rfc/weakrefs I guess the only other thought is I'll have a look and see if we can refactor the code at all to remove the circular reference... but given the issue has gone undetected for so long, I might just have to use some raw SQL queries on my particular use case to as an intrim solution... 😢 |
Do your queries use the If not, you could create your own |
It looks like we can fix this by storing the parent query's connection, grammar, processor and class name separately instead of the whole query object. |
Yeah, I dont use either, so that seems to help in this instance.
How do we get around the call to edit: oh - if we change the constructor, i guess that works... |
The only downside is the breaking change. I'll submit a PR. |
Just target 5.9 I guess? It's been here since 5.3, so it's not like a new urgent bug...? edit: I've changed my script and added the |
Description:
There seems to be a runaway memory leak when using any of the
join
DB functions.Steps To Reproduce:
On a clean Laravel 5.8 install just drop this inside your
console.php
:Running
php artisan static-memory-test
shows:But running
php artisan leak-memory-test
shows:I noticed this, because I have a very complicated SQL query with lots of joins on a continuous loop as part of a long-running process. The script would keep crashing after just a few minutes saying it was out of memory. After many hours I managed track down the cause to the
join
statements.What I've been able to work out is any/all join statements are affected -i.e.
join()
,leftJoin()
etc.I did some further testing, and discovered the bug does NOT exist in either Laravel 5.1 or Laravel 5.2. In both those versions you can run a loop with a
join
and the memory usage remains static, so we know it can be done.But on Laravel
5.3.0
the memory usage issue appears. I think I've tracked down the PR that caused this to a refactor submitted here: #13576The problem is there have been so many code changes and other PRs since then, it's difficult to work out the solution here.
Ping @acasar (original PR contributor)
Ping @staudenmeir (resident DB expert)
The text was updated successfully, but these errors were encountered: