Fix this error
PHP: Allowed memory size exhausted
Dasun Kalupahana 145 views
The error
Error
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)
Why it happens
The script used more memory than PHP's memory_limit (134217728 bytes = 128 MB). Most often it loads too much at once, like Model::all() on a big table, or builds a huge array or file in memory. Composer can hit it too.
How to fix it
- Process big tables in pieces with
chunk(),chunkById()orlazy()instead ofall()/get(). - Only select the columns you need.
- If the job really needs more, raise
memory_limitinphp.ini(or per script). For Composer:COMPOSER_MEMORY_LIMIT=-1 composer update.
// Instead of User::all()
User::select('id', 'email')->chunkById(500, function ($users) {
foreach ($users as $user) {
// ...
}
});
Learn more on the blog
PostgreSQL vs MySQL Performance: Real Benchmarks Compared
Last updated: 7 November 2026. See the update log at the end."Which...
How to Detect and Fix PHP Memory Leaks
Last updated: 5 November 2026. See the update log at the end.In...
Docker Build Too Slow? 10 Ways to Speed It Up
A slow Docker build usually has one of a few causes: you...