Memory Allocation, Bytes Exhausted PHP/LARAVEL

laravel command allowed memory size of 134217728 bytes exhausted
laravel memory limit
fatal error: allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes)
allowed memory size of bytes exhausted nginx
phpunit allowed memory size exhausted
php fatal error: allowed memory size of 100663296 bytes exhausted
memory limit exceeded php
fatal error: allowed memory size of 1610612736 bytes exhausted

Hey guys,

I am developing a system using Laravel Excel/Maatwebsite. What I'm trying to achieve is when a user inserts an excel file into the system, the system will check for a few things and then insert the data into the database.

Here is an instance of the database schemas:

h_id | h_english | r_id | h_last_raavi_id | b_id | s_id | k_id | h_status

r_id | r_english | r_status

b_id | b_english | s_id | b_status

s_id | s_english | k_id | s_status

k_id | k_english | k_status

My controller:

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Section;
use App\Models\Raavi;
use App\Imports\HadeesImport;
use Excel;

class ImportHadeesController extends Controller{
     * Show the application import-hadees page.
     * @return \Illuminate\Http\Response
    public function index(){
        $section = Section::where(['s_status' => 1])->get();

        return view('admin/import-hadees', compact('section'));

     * This method uses the Excel facade to prep the excel file 
     * and extract data from it and uses App\Imports\HadeesImport 
     * class to insert each row in the database schema accordingly.
     * @param Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
    public function importHadees(Request $request){
        $raavi = Raavi::where(['r_status' => 1])->get();

        if($request->file('hadees_sheet')) {
        } else {
           return response()->json(['status'=>'error', 'msg'=> 'No file present!']);

        $temp = $request->file('hadees_sheet')->store('temp'); 

        $hadees = Excel::import(new HadeesImport($request->s_id, compact('raavi')), $path);

            return response()->json(['status'=>'success', 'msg'=> 'Successfully imported all the data from the file to the database!']);
        } else{
            return response()->json(['status'=>'error', 'msg'=> 'Unable to import data from the file to the database!']);

HadeesImport Class:

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use App\Models\Section;
use App\Models\Baab;
use App\Models\Hadees;
use App\Models\Raavi;

class HadeesImport implements ToCollection, WithHeadingRow{
     * Global variable for section_id.
    public $s_id;

     * Global variable for raavi's data.
    public $raavi;

     * Construction function.
     * @param int $id
    function __construct($id, $arr) {
        $this->s_id = $id;
        $this->raavi = $arr;

    * This method is responsible for inserting the excel
    * sheet's rows data to the database schema.
    * @param Collection $row
    public function collection(Collection $rows){
        $baab = Baab::where(['s_id' => $this->s_id])->get();
        $hissa = Section::where(['s_id' => $this->s_id])->first();
        $kitaab = $hissa->k_id;
        $first_raavi = 0;
        $last_raavi = 0;
        $baab_id = 0;
        $data = array();

        foreach ($rows as $row){
            if($row['hadees_arabic'] != "" && $row['hadees_urdu'] != ""){
                $baab_id = $this->baabCheck($baab, $row);

                foreach($this->raavi['raavi'] as $rav){
                    if(trim($rav->r_english) == trim($row['first_raavi_english'])){ 
                        $first_raavi = $rav->r_id; 
                    } else{
                        $first_raavi = 0;

                    $last_raavi = (trim($rav->r_english) == trim($row['last_raavi_english']))? $rav->r_id : 0;

                if($first_raavi == 0){
                    $raavi = Raavi::create([
                        'r_arabic' => trim($row['first_raavi_urdu']),
                        'r_urdu' => trim($row['first_raavi_urdu']),
                        'r_english' => trim($row['first_raavi_english']),
                        'r_nickname' => trim($row['raavi_other_names']),
                        'r_status' => 1,

                    $first_raavi = $raavi->r_id;

                if($last_raavi == 0){
                    $raavi = Raavi::create([
                        'r_arabic' => trim($row['last_raavi_urdu']),
                        'r_urdu' => trim($row['last_raavi_urdu']),
                        'r_english' => trim($row['last_raavi_english']),
                        'r_nickname' => 'n/a',
                        'r_status' => 1,

                    $last_raavi = $raavi->r_id;

                $data = array([
                    'h_arabic' => trim($row['hadees_arabic']),
                    'h_urdu' => trim($row['hadees_urdu']),
                    'h_english' => trim($row['hadees_english']),
                    'h_roman_keywords' => trim($row['roman_keywords']),
                    'h_number' => trim($row['hadees_number']),
                    'r_id' => $first_raavi,
                    'h_last_raavi_id' => $last_raavi,
                    'b_id' => $baab_id,
                    's_id' => $this->s_id,
                    'k_id' => $kitaab,
                    'h_status' => 1

        $hadees = Hadees::create($data);

    * Checks if the baab exists in the database or not.
    * @param Collection $baab
    * @param Object $row
    * @return int - baad_id or 0
    public function baabCheck($baab, $row){
        foreach($baab as $b){
            if(trim($b->b_arabic) == trim($row['baab_arabic']) || trim($b->b_urdu) == trim($row['baab_urdu']) || trim($b->b_english) == trim($row['baab_english'])){
                return $b->b_id;
            } else{
                return 0;

It all was working fine when the data was less. Now I have 1400+ rows in Raavi table and 10,000+ rows in Baab table. Now whenever I try to import a sheet to the system it gives me this error:

Allowed memory size of 268435456 bytes exhausted (tried to allocate 37748736 bytes).

I think it's because of so long foreach() loop. Any kind of help would be highly appreciated. If you guys have any suggestions about bad coding or bad logic building, please do let me know. I have been stuck on that issue for almost two days. Thanks in advance.

P.s: The error is same on the localhost and on hosting, just a difference in bytes. Which is due to different memory_limit setting, I believe.

All posted solutions mention raising the memory limit for PHP.

It does not work like that. You can't just throw more RAM at a problem. What if your server has 2GB of RAM and you upload a file that, with all the arrays created there, can use more than 2GB of RAM? What's next? Not to mention the risk of the server running out of memory and killing other processes. For example, if the server has shared PHP and MySQL running and PHP is causing the server to run out of memory, the OOM Killer will kick in and might kill the MySQL process.

The solution to your problem is to process that file in chunks. Laravel 6, for example, has a new type of collection, Lazy Collections. They can help you speed things up. Your code might have to change in order to use chunk processing, but imho, that is the only way you can fix this problem.

I would also run this in a queue, not from a user request.

I also know that the package you are using supports both chunking for reading and batching for inserting.

Memory Allocation, Bytes Exhausted PHP/LARAVEL, All posted solutions mention raising the memory limit for PHP. It does not work like that. You can't just throw more RAM at a problem. What if  We just moved our code to the latest build of Laravel (from a pre 5.0 version) to get ready for the 5.1 release and the unit tests are now failing due to a memory allocation problem. The memory allocation problem does not occur when running each tests class separately (not running the comprehensive test suite).

Extend your memory_limit. For 'localhost', in php.ini-


And restart your server.

PHP Fatal error: Allowed memory size, exhausted (tried to allocate 4096 bytes) in C:\xampp\htdocs\inteligencia\vendor​\laravel\framework\src\Illuminate\Database\Grammar.php on  COMPOSER_MEMORY_LIMIT=128MB php composer.phar update Use the format “128M” for megabyte or “2G” for gigabyte. You can use the value “-1” to ignore the memory limit completely.

You can set the memory_limit for certain files/pages/scripts to be more than what php.ini sets. Add a __construct() method and raise your memory limit:

public function __construct() {

     ini_set('memory_limit', '1G'); // change as needed, as long as your system can support it

     parent::__construct(); // If added in your controller. Probably not needed if you use it in your import class


Laravel 5 PHPUnit Memory Allocation Exhausted, PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 53 bytes) in /home/vagrant/www/api.lawnstarter/vendor/routes.php on  One of the most common and frustrating errors encountered by PHP coders reads: “Fatal error: Allowed memory size of 8388608 bytes exhausted…” followed by something like “(tried to allocate

Set the memory_limit to be unlimited for a specific file to override php.ini memory_limit

// put it in your construct 

ini_set('memory_limit', -1);

PHP memory limit, that moment happens: Fatal error: Allowed memory size of a certain amount bytes exhausted (tried to allocate another amount bytes) in /path/to/script.php  can someone help me this? I running composer require illuminate/support in ssh server but it show Fatal error: Allowed memory size of 1610612736 byte

Fixing PHP Fatal Error: Allowed Memory Size Exhausted, PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB the application working again for the purpose of then reducing the memory usage​. The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

in laravel Allowed memory size of 134217728 bytes exhausted (tried , Permanently You can permanently change the PHP memory allocation two ways. When you see the above error - especially if the (tried to allocate __ bytes) is  Same problem here. I can't get it to composer update as I only have 1gb total for this particular vps. PHP 5.4.11 (cli) (built: Feb 20 2013 19:02:54)

PHP Out of memory issue · Issue #7 · olssonm/laravel-backup-shield , PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1073741832 bytes). I'm using ubuntu 16.04 in  In my WHMCS dev environment, all at once crons stopped working regularly. Looking for errors, I found several Allowed memory size of 33554432 bytes exhausted: [25-Jun-2020 00:01:01 UTC] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 139264 bytes) in /home/devd