Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug "Fatal error: Allowed memory size of xxx bytes exhausted" in PHP

Tags:

php

What is the best strategy to debug a "Fatal error: Allowed memory size of 268435456 bytes exhausted " error? This error i'm getting is strange and something is obviously wrong. The function which is causing it is

/**
 * Flush all output buffers for PHP 5.2.
 *
 * Make sure all output buffers are flushed before our singletons our destroyed.
 *
 * @since 2.2.0
 */
function wp_ob_end_flush_all() {
    $levels = ob_get_level();
    for ($i=0; $i<$levels; $i++)
        ob_end_flush();
}

i simply rebased some code i was working on and started getting this. what's your strategy to debug this?

like image 969
Nicola Peluchetti Avatar asked Dec 10 '13 22:12

Nicola Peluchetti


People also ask

How do I fix PHP fatal error allowed memory size?

You can find the php. ini file in the public_html for your website and right-click on the file to Edit it. Look for the line defining the memory_limit variable and set the value accordingly. Then, save the changes and reload your site to see if the PHP “Allowed Memory Size of Bytes Exhausted” error has been resolved.

How do I fix fatal error allowed the memory size of 134217728 bytes exhausted?

The correct way is to edit your php. ini file. Edit memory_limit to your desire value. As from your question, 128M (which is the default limit) has been exceeded, so there is something seriously wrong with your code as it should not take that much.

What is a fatal memory error?

If you've been getting an error that says “Fatal error: Allowed memory size of x bytes exhausted”, that means either your server is limiting the amount of memory used by your website or a plugin is consuming too much server memory.


2 Answers

Try the below code, if your code reaches the specified number of bytes it just echo it and exit. instead of crashing :

function wp_ob_end_flush_all() {
    $levels = ob_get_level();
    for ($i=0; $i<$levels; $i++){
        ob_end_flush();
        if(memory_get_peak_usage() > 268435400) { // 268435456 
            echo memory_get_peak_usage(). ' reached! now we should stop the script.' ;
            break; // or die();
        } 
    }
}

Update To answer your question, one way to debug leaking is to use xdebug another way it to use the function I gave in the example or wrap your suspicious functions by memory_get_usage and compare the difference.

like image 127
Mehdi Karamosly Avatar answered Oct 23 '22 09:10

Mehdi Karamosly


I was also getting this error upon starting the Apache server with EasyPhp-Devserver-16.1.

In my case, it was because Easy php was trying to load a too large error.log file.

Deleting the old server log in

C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-binaries\httpserver\apache2418x160331124251\logs

and creating an empty one solved my problem.

Hope this can help others.

like image 36
Pierre C Avatar answered Oct 23 '22 07:10

Pierre C