Imagine, that we have a situation where we need to make a lot of queries with Eloquent in a loop. Something like this:
Here in an endless loop, we grab records from out statistics and process them. But what happens if this loop will work too long. Soon you will find out that you are out of memory. But why? PHP memory leaks? Ok, lets force to run a garbage collector after every iteration:
This doesn’t help, the memory is out again. Maybe we can force to unset out variables:
This doesn’t help too. The memory is still leaking. After some research, I have found out that it was Eloqunet. Eloquent logs all the queries by default. To disable this behavior you can do this:
And it you are using Eloquent outside of Laravel:
@ 2020. All rights reserved.
If you've got any feedback, comments or just want to chat you can get in
touch via Twitter or email.