How to Increase PHP Memory Limits
Published on by Eric L. Barnes
Whenever you set up a new PHP project, some memory is allocated automatically. This memory is mostly suitable for general applications. But there are some cases, for example when you are loading some heavy images when your website is using high graphics then you get some errors like -
“Fatal error: Allowed memory size of xxxxxx bytes exhausted”
“filename.jpg exceeds the maximum upload size for this site.”
The best way to solve this error is to contact the hosting provider and increase the memory limit of the application. But there are ways to increase the memory limit of the whole website or a particular script without any expert help like using php.ini file, .htacess file, etc. In this blog, we will discuss the various strategies for increasing memory limit and the benefits of increasing the memory limit of your PHP application.
What is the PHP memory limit?
PHP memory limit is per script memory allotted to the PHP script. It is the same as the storage limit a particular task can occupy. This memory limit in PHP scripts is useful in some cases. For example, there can be cases when some poorly written code tries to eat up all the memory in the stack.
Most WordPress websites have a memory limit of 32M, but you may require more memory in many cases. For example, if you are doing heavy operations like recurring calls to the database and heavy image processing, you need to increase the memory limit of your script.
Strategies to Consider
In this part of the blog, we will share various ways to increase the memory limits of your PHP scripts/apps. While these are not the only ways to increase the memory limits of your PHP script, these are the ideal steps that most developers use for memory limit issues.
Also, changing the memory limit of an app can create problems sometimes, so you should always back up the data of your system.
Before trying to increase memory, you should always talk to the server providers of your website. They can help you increase the memory limit using their best practices.
Strategy 1: Edit the PHP.ini File
The php.ini file is executed every time a PHP application runs, and it’s used for controlling the various settings of PHP script like maximum upload size, memory limit, timeout limit, etc.
To increase the memory limit, you can change the following variables. But beware, these variables are case sensitive, and you need to restart the server after doing changes for them to be reflected.
memory_limit = 256Mupload_max_filesize = 12Mpost_max_size = 13Mfile_uploads = Onmax_execution_time = 180
The max execution time refers to the timeout of the PHP script, and it means the maximum time for which the screen can be run.
Strategy 2: Edit The HTAccess File
The .htacess file is a secret file hence its name starts with a dot. If you are using the shared hosting or, for some reason, you cannot access the php.ini file, you need to edit the .htacess file to increase the memory limit.
There are various use cases of this .htacess file. You need to add the following lines to this file to increase the memory limit.
php_value memory_limit 256Mphp_value upload_max_filesize 12Mphp_value post_max_size 13M
Strategy 3: Increase via ini_set() Function
The ini_set() function is used to set the value of a particular attribute in the script's context only. It is considered the safest of all the above ways because it only sets the value for the script particularly and restricts the poorly written scripts from consuming all the memory on the server.
To use this function to increase the memory limit, you can simply do.
The above function will set the memory limit at 512 MB. Also, the ini_set() function is used only to set the value of a variable temporarily; once you close the script and restart it, it will take original values from the php.ini file.
There are many common errors like this memory limit one. In this blog, we have discussed the different strategies for increasing the memory limit in your script. The point to be noted here is that you should always raise the memory limit of your PHP script only as a last resort because this is a crucial task and can impact your site in many ways.
By the way, after making your website live to the actual customers, you must want to know what your users are doing on your website and the difficulties they are facing. It will help in increasing engagement and customer retention also. Scout APM is the best modern solution for the monitoring of your PHP application. It comes with support for many languages like PHP, Ruby, Python, etc. You will get 24x7 customer service, and pricing is very low compared to the market competitors.
We also offer a 14-day free trial without any credit card. So if you want to scale your business to the next level, start your free trial now!