Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes)

87,470

Solution 1

Default php.ini memory_limit is 128 MB. You should either:

  • Optimize your code to use a normal amount of data
  • change memory_limit in php.ini to higher value which I do not recommend at all - with your approach you will hit this wall once again

Solution 2

You can expand your memory from within your file with the following line of code:

ini_set('memory_limit','16M');

Add this code to the top of your file and ahange 16M to be whatever you need it be and that should do it.

Share:
87,470
Abdul Rahman
Author by

Abdul Rahman

Web Developer having experience +9 Years. Specialty: jQuery, JavaScripts, PHP, JSP, ASP 3.0 (Classic), ASP .Net with C#, Java Programming Language, HTML, CSS.

Updated on July 05, 2022

Comments

  • Abdul Rahman
    Abdul Rahman almost 2 years

    I am retrieving record from mysql table which return more than 0.2m number of rows per query, which obviously take lot of memory. in my case i have 8 GBs installed RAM on my system with SSD 256 GBs. When i execute my page it returns the following error:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes) in D:\xampp\htdocs\classes\CRUD.php on line 84   
    

    I think i should need to use threading instead of php loops over table rows? Maybe i am wrong. Any suggestion/help will be appreciated.

  • Abdul Rahman
    Abdul Rahman about 8 years
    what about => ini_set('memory_limit','2G'); will it work? or there should be any other good idea?
  • Tom Cash
    Tom Cash about 8 years
    You can go higher, but as Maciej Asembler said, it is not advised. My solution is a quick fix. Ideally, you need to streamline your application.
  • Maciej Asembler
    Maciej Asembler about 8 years
    You should also check if you can invoke ini_set anyways.
  • Ashiq
    Ashiq over 4 years
    In case anyone with laravel project wonder where to put it! If the code is executing in web page call, then you need to put it in index.php in the very first line after <?php starts. If the code is running from any artisan command like queue, then you need to add this in artisan file in same position as told before.....
  • Fokwa Best
    Fokwa Best about 4 years
    Hi @AbdulRahman, I am facing thesame problem like you. I have increased to 500MB but still not enough. How did you finally solve the issue in an effective manner ?
  • Abdul Rahman
    Abdul Rahman about 4 years
    @FokwaBest i changed my code style- I send the request with json and returned the json encoded array of values only. which did two things for me: 1- Increased the speed of the response 2- minimized the size of the response from huge size to some MBs which was the actual requirement and solution for me. i will advise you to not increase the size of the memory limit as it is not the solution of the problem.