- How to Increase PHP Memory Limits
- What is the PHP memory limit?
- Strategies to Consider
- Strategy 1: Edit the PHP.ini File
- Strategy 2: Edit The HTAccess File
- Strategy 3: Edit your wp-config.php File (If Working in WordPress)
- Strategy 4: Increase via CPanel
- Strategy 5: Increase via ini_set() Function
- Key Takeaways
- Most popular
- PHP memory_limit – understanding and increasing this setting
- PHP memory_limit is a per-script setting
- How to increase PHP memory_limit
- How to increase PHP memory limit
- Increase PHP memory limit via file (php.ini)
- Increase PHP memory limit via .htaccess
- Increase PHP memory limit via ini_set() function
- Increase PHP memory limit for cPanel
How to Increase PHP Memory Limits
Why are PHP memory limits important to your website development journey? PHP is a famous backend technology that is used by many tech giants for supporting their applications. PHP gives many advanced features for making web pages dynamic and integrating some features you can not simply get using javascript, HTML, and CSS.
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” and
“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 = 256M upload_max_filesize = 12M post_max_size = 13M file_uploads = On max_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 256M php_value upload_max_filesize 12M php_value post_max_size 13M
Strategy 3: Edit your wp-config.php File (If Working in WordPress)
Wp-config.php is one of the most critical files in WordPress sites. It is the configuration file of the WordPress sites.
For the memory limit, you can find a variable named WP_MEMORY_LIMIT in the config. Generally, the value of this is 32M. But you can increase the limit by altering this variable. For example, you can do something like this:
define('WP_MEMORY_LIMIT', '64M');
You can increase the memory to whatever limit you want and just save and close the config file to roll out the changes.
Strategy 4: Increase via CPanel
cPanel contains most of the information that is needed to run the website. If you cannot increase the memory limit using any one of the above methods, you need to increase it via the cPanel admin dashboard. So if you have access to cPanel, then to change the memory limit, follow the given steps:
- Login to your cPanel admin dashboard. Then select the PHP version of your website.
- Now go to options for this PHP version and find the memory limit column there.
- There you can change easily change the memory limit of your PHP script, and your changes will be automatically saved.
Strategy 5: 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.
ini_set('memory_limit', '512MB');
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.
Key Takeaways
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 24×7 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 !
Most popular
PHP memory_limit – understanding and increasing this setting
PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle. For example, although PHP’s memory limit may be set high to 1GB, that does not mean that scripts will pile up to use that 1GB. Let’s take a quick look at understanding PHP’s memory_limit setting.
PHP memory_limit is a per-script setting
PHP.net’s documentation puts it this way:
This 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.
Unlike, say, MySQL’s key_buffer_size or innodb_buffer_pool settings, PHP memory_limit setting is not a storage space where multiple PHP scripts pool from or grow within. Rather it’s a per-script limit. PHP memory_limit is the maximum amount of server memory a single PHP script is allowed to consume. When blocked, the resulting error output looks something like this:
Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script
So, for example, if two or more scripts are requested simultaneously, each is completely independent of the other. They do not share the memory_limit setting. Remember, PHP is not designed for and does not support multithreading. Thus, if five (5) PHP scripts simultaneously use 100MB each, that would total 500MB of PHP memory usage, and a PHP memory limit of 128M wouldn’t be hit.
That said, for scripts that request other PHP scripts inline using require, include, or include_once, this limit is then inherited and shared by all included scripts that are dependent on the parent script.
“The include statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.” – W3schools.
“The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.” – php.net
PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle.
Now regarding the original example mentioned at the outset. A lower setting of 128M is always better because if PHP scripts are trying to use more than 128M, those scripts would now return memory limit exceeded errors. In the above issue, that was not the case, so regardless of the 128M or 1G memory_limit setting, it only comes into play if there are inefficient scripts.
Fortunately, the PHP memory_limit setting will block inefficient code, alerting you to optimize your code. Until fixed, you may want to temporarily increase PHP memory_limit to avoid your web application becoming unusable due to PHP out-of-memory errors.
If you don’t have available memory on your server, you’ll sometimes be faced with deciding whether to increase PHP memory_limit to meet the requirements of scripts or to optimize your code. It would be best if you always optimized as the preferred option when possible. Also, you can increase PHP’s memory limit for specific websites. One method would be to place a php.ini file in the site’s webroot. You can even set the limit for specific scriptname.php. For example using ini_set(‘memory_limit’,’256MB’).
How to increase PHP memory_limit
To increase the PHP memory limit setting, edit your PHP.ini file. Increase the default value (example: Maximum amount of memory a script may consume = 128MB) of the PHP memory limit line in php.ini.
Alternatively, you can edit your .htaccess file (Not recommended. See: Apache Performance: Disable .htaccess)
php_value memory_limit 1024M
If you don’t have access to these files or lack the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.
Originally published: Sep 18th, 2017.
Last updated: February 6th, 2023.
How to increase PHP memory limit
You can increase the memory limit if your PHP scripts require to avoid the error. Some methods to increase the memory limit in PHP are configuring php.ini or .htaccess file and using ini_set() function. cPanel provides the option to configure PHP memory limit from the web interface.
Methods to increase PHP memory limit:
Increase PHP memory limit via file (php.ini)
This method will apply to all the PHP scripts that run in the system. It is a good method to follow if you’re not hosting multiple systems in a single host.
$ sudo vi /etc/php/7.2/apache2/php.ini
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 128MB
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 512MB
Increase PHP memory limit via .htaccess
To make the setting effective only to a certain folder (or project), you can use the .htaccess file to do the trick. To do this, locate (or create) .htaccess file in your PHP scripts folder and add the following line;
php_value memory_limit 512MB
Increase PHP memory limit via ini_set() function
It is probably the best method as the setting applies only to the particular script and would not allow other poorly written scripts to consume and waste the system’s memory.
To do this, add the following line in your PHP script;
Increase PHP memory limit for cPanel
You don’t have access to PHP‘s configuration file if you’re hosting your system in cPanel, but cPanel do provide the option to change the setting from its admin dashboard.