I use eloquent model to do some complex migration of database and run out of memory during the processing. Can someone explain what's the reason? Thank you!
Laravel version: "v8.52.0"
Test code:
public function handle()
{
for ($i = 0; $i < 100; $i++) {
Customer::chunkById(1000, function ($customers) use ($i) {
$this->print_progress();
foreach ($customers as $customer) {
$customer->first_name = (string)$i;
$customer->save();
}
});
}
}
Output (memory usage):
usage: 27MB - peek: 27MB
usage: 33MB - peek: 33MB
usage: 39MB - peek: 39MB
...
...
usage: 491MB - peek: 491MB
usage: 496MB - peek: 496MB
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/vagrant/code/billing/vendor/laravel/framework/src/Illumi nate/Support/Str.php on line 855
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 32768 bytes) in /home/vagrant/code/billing/vendor/symfony/error-handler/Error/ FatalError.php on line 1```
Update: Memory leak is caused by telescope. When turn off telescope, no memroy leak occurs.