-
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
Memory leak during bulk insert using DB::table('table_name')->insert() after upgrading to laravel 8 from 5.8 #35095
Comments
This would need a minimum reproducible example. Is it possible that a debug screen like Ignition is causing the memory leak? |
@taylorotwell Not it's not because of that for sure. I am getting this on seeding a database from a file. |
|
|
@taylorotwell These are the two classes One seeder and one another class which is reading from the TSV file every 1000 entries. When I comment out the DB::table()->insert() statement, memory rise doesn't happen and is consistent. But if included, after every rise memory rise happens. I had the same code on laravel 5.8, but everything was working fine and no issues. I updated to laravel 8 and then faced this issue on seeding. |
You can simplify code by using LazyCollection and try with less chunk size.
class SeederBigFiles extends \Illuminate\Support\LazyCollection
{
public function __construct($filename, $delimiter = "\t")
{
$file = fopen($filename, 'r');
$header = fgetcsv($file, 1000, $delimiter);
parent::__construct(function () use ($file, $delimiter, $header) {
while (($row = fgetcsv($file, 1000, $delimiter)) !== false) {
yield array_combine($header, $row);
}
fclose($file);
});
}
}
(new SeederBigFiles($fileName))
->map(function ($entry) {
//prepare data
return $entry;
})
->chunk(100)
->each(function($data) {
DB::table('service_list')->insert($data);
}); |
@dkulyk But why is my memory size increasing on every chunk addition, that's the point. If I extract 1000 rows and give it to seed, for every 1000 it should remain nearly the same. But for every chunk inserted, the memory never gets free, it just keeps on increasing. Secondly, this was not the issue on the Laravel 5.8. So why on laravel 8? |
maybe you added a telescope or enabled query log or something like a debug panel |
@stockarea have you tried disabling Telescope and Ignition to see if that solves it? |
Is facade/ignition added by default because of laravel 8. And if yes how to disable that by default. |
Do you know how much does the memory usage differ between Laravel 5.8 and 8? Few memory intensive usage:
Using LazyCollection as suggested by @dkulyk can make a huge different on memory usage. |
@stockarea just remove it from your composer dependencies |
@driesvints that i have not installed then. Can send you composer.json if needed. But have not installed it. |
@crynobone This lazy collection will implement and tell about it |
@stockarea feel free to report back later when you've evaluated it 👍 |
okay |
@driesvints Please see this.
I went back to the previous commit where laravel 5.8 was installed. Updated through composer. And ran the same script for seeding. Memory is consistent and its only 13MB. While this is for the current laravel 8 version
It's the same code on both the cases. |
@dkulyk I am not using facade/ignition package. Only using laravel-debugbar which does not work on cli mode. Here is my composer.json
|
@driesvints Will see removing facade/ignition also. My bad didn't saw in require-dev |
@driesvints Too sorry. It was because of facade/ignition only. Laravel shift added it on require-dev which I missed. Sorry for wasting time. |
Man, you didn't waste the time, You saved the time, I took 24Hour to resolve this but was no luck, then found your post, which almost solved my issue, yes it was a memory leakage |
I'm in the same situation. By the way, could the
I removed the Also, I didn't add an event to trigger when data is inserted into the model. The I got a hint from your comments. Thank you. |
Now I'm getting this issue in Laravel 11, though it's caused by something new. See #52416 |
|
I had a memory leak when inserting data in a for loop because I had telescope turned on. |
Description:
I was reading a TSV file in chunks (1000 rows ) and inserting into the database. Earlier it never caused any issues. I continuously monitored the memory usage every time the command run. It was well under 30mb.
After upgrading to laravel 8, running the same command first gave me memory exhaustion error (memory_limit = 128M set on php.ini file).
Using Laravel Models was also giving the same issue.
Steps To Reproduce:
The text was updated successfully, but these errors were encountered: