202 points
12 6 5

 What is PHP Memory Limit:

This directive sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. This helps prevent poorly written scripts for eating up all available memory on a server. More information about this directive is available here


The error occurs when your script requires more memory than the one specified in your php.ini file. The solution is to adjust your PHP’s directive named memory_limit. Please note this is also possible that your memory_limit value is beign ignored by PHP because of incorrect syntax. If you specify this value using the correct syntax this should solve this problem. This article also deals with some most common incorrect syntax used and the correct syntax.

memory_limit Correct Syntax:

; When an integer is used, the value is measured in bytes
memory_limit = 262144 ; 

When K is suffixed after an integer value, the value is measured in Kilobytes
memory_limit = 256K ; 

When M is suffixed after an integer value, the value is measured in Megabytes
memory_limit = 256M

Some Common Incorrect Syntax:

memory_limit = 128MB

memory_limit = 128KB

memory_limit = 2561442 Bytes

There are following ways to increase the value of memory_limit.

1- Edit php.ini file directly:

This solution is for those who are running a server on localhost. If your operating system is Windows, you can locate your php.ini file from this location. C:\xampp\php\php.ini. It is assumed that your xampp installation is located in C Drive on your Windows PC. If not, then locate your xampp installation on your PC and navigate to /php/php.ini. If you are using Linux, then most distributions put lampp under /opt/lampp, so the file can be found under /opt/lampp/etc/php.ini.

Open your php.ini file and then search for the term memory_limit. Change its current value to whatever you want to set. Below I am showing a portion of my php.ini file that contains this directive.

; Maximum amount of memory a script may consume
memory_limit = 128M

Note: After making changes to php.ini file, you have to restart the apache server. By this setting, the configuration is made global to all PHP scripts. Changes done to this file in an incorrect way can create problems to the web-server or live projects. To restart your apache server (if its already running first close it then) click on start button next to it. Now your changes in php.ini file should get effective.

2- Edit php.ini on remote server:

If its a remote server, then login to your Cpanel. The interface of Cpanel can be different for different hosting companies but you the basic idea is always the same. Look for PHP settings inside Cpanel dashboard, then look for the term memory_limit. Set up your desired value and save it.

3- Using the PHP built-in function:

If somehow you are not able to use any of the above methods to increase the memory_limit, you can do so using your PHP code. Place this code at the start of your PHP script so it applies before your script executes your main code.


    ini_set('memory_limit', '128M');  //I have placed 128M, feel free to change this value as per your requirements

4- Using htaccess file:

This method will globally update the directive value which means all your scipts running under this htacces file will have this value.

php_value memory_limit 128M #feel free to change the value as per your requirements









More Posts

PHP fatal error allowed memory size of 262144 bytes exhausted IvanLomakin - Aug 4
Fatal error: The allowed memory size of 41943040 bytes exhausted. Farkhanda Athar - Apr 28, 2020
PHP fatal error: maximum execution time of 30 seconds exceeded IvanLomakin - Aug 4
Php fatal error: maximum execution time of 30 seconds exceeded ahsanhanif99 - Aug 3
PHP fatal error: uncaught error: call to undefined function mysql_connect() Farkhanda Athar - Apr 19, 2020
Fatal error Maximum execution time of 30 seconds exceeded in xampp. Farkhanda Athar - Apr 25, 2020
Fatal error: require_once(): failed opening required. Farkhanda Athar - May 7, 2020
Parse error syntax error, unexpected end of file php IvanLomakin - Aug 4
PHP parse error syntax error unexpected end of file in IvanLomakin - Aug 4
Syntax error unexpected end of file PHP. Farkhanda Athar - Jul 21, 2020