If you have ever seen this in a PHP log, you know the feeling:
Fatal error: Allowed memory size of 134217728 bytes exhausted
Enter fullscreen mode
Exit fullscreen mode
The script stops dead. There is no graceful fallback, no catch block that saves it. And the usual first reaction — raising memory_limit — often only buys time. If the job keeps growing, it will hit the new limit too.
Here are five mistakes I see again and again, and the fix for each one.
1. Loading every row with get() / all()
This is the classic one. A report, an export, or a migration loads an entire table into an array before it does any work:
// Bad: every user in memory at once $users = User :: all ( ) ; foreach ( $users as $user ) { // ...…