PHP: Disable memory limit.

This is a short guide on how to completely disable PHP’s memory limit value.

The other day, I was creating a huge PHP report that pulled millions of rows of data from MySQL. As a result, I quickly ran into the dreaded memory exhausted error.

Because this was a once-off custom report and I was not running it on a live server, I was able to use the following “hack”:

//Disable memory_limit by setting it to minus 1.
ini_set("memory_limit", "-1");

//Disable the time limit by setting it to 0.
set_time_limit(0);

The PHP code above disables the “memory_limit” configuration directive. This is a per-script configuration value.

Note that this should never be used on a production server. Otherwise, your script could potentially use up all of the memory that PHP has allocated to it.

If the above “hack” fails and you still receive a memory error, then it is probably because your script has used up all of the memory that PHP can use. That, or you’ve completely depleted the amount of RAM on your Operating System.

If that is the case, then you will need to run the report on a better server or you will need to process the data in chunks instead of loading it all into memory.

Hopefully, this helped!