- 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
- How to modify PHP memory limit
- Take your skills to the next level ⚡️
- About
- Search
- Tags
- PHP ini_set Memory Limit
- Set Memory Limit Using PHP Script
- Set Memory Limit Using Php.ini File
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
How to modify PHP memory limit
Posted on Aug 09, 2022
PHP has a memory limit configuration that determines the maximum amount of memory a single PHP script can consume.
This memory_limit is usually defined in a php.ini file as follows:
The default memory_limit configuration for PHP is 128M or 128 Megabytes. You can replace this value in your php.ini file with any other int value.
Without the M symbol, the limit will be measured in bytes:
The memory limit in PHP is calculated on a pre-script basis, meaning that each PHP script request is independent of the other.
For example, if you have 3 running PHP scripts where each consumes 90MB of memory, then the total memory used by PHP will be 270MB passing the 128MB limit.
When you have a single PHP script that consumes 200MB of memory, then PHP will throw an Out of memory fatal error.
To remove the memory_limit , you can set the value as -1 like this:
This way, PHP won’t have memory limitations and can consume all available memory on your server.
Aside from editing the php.ini file, you can also edit the .htaccess file as shown below:
You can get the current memory_limit value with the ini_get() function:
You can also change the memory limit configuration only for a specific script using ini_set() function:
And that’s how you set the memory_limit configuration in PHP.
- WP_MEMORY_LIMIT sets the memory limit in WP frontend area
- WP_MAX_MEMORY_LIMIT sets the memory limit in WP administration area
The WP_MEMORY_LIMIT is usually set at 64MB for WP multisite and 40MB for a single site, while WP_MAX_MEMORY_LIMIT is usually set at 256MB.
You can define these constants in wp-config.php file, below the WP_DEBUG definition as shown below:
WordPress configuration doesn’t override the PHP configuration, so make sure you have memory_limit at 300MB or more when you set WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT at 300MB.
That’s all you need to change for a WordPress site.
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with:
PHP ini_set Memory Limit
- Set Memory Limit Using PHP Script
- Set Memory Limit Using Php.ini File
Although there are many ways to set memory limits in PHP, the usual way is to modify the php.ini file where the php version is installed on the user machine.
However, it depends on what users want to do with memory modification because the default memory limit allocated for variable consumption is sufficient to execute most tasks.
Set Memory Limit Using PHP Script
We will first echo ini_get(«memory_limit») to check what is the current memory limit. Then, we will use ini_set(«memory_limit»,»»); to modify the memory limit.
This method will only apply to the currently used files. And it is also the best practice to use this kind of script rather than affecting the memory limit in the php.ini or .htaccess files, which might cause other problems concerning memory later.
The practical solution to this issue is to create a file, add the following script, and use it to modify the default memory.
php //check memory limit using ini_get echo "Current Memory:"; // this will print your current memory limit echo ini_get("memory_limit")."
"; //set memory using ini_set //format is very important ini_set("memory_limit","256M"); //print new memory limit echo "Updates Memory:"; echo ini_get("memory_limit"); ?>
We initially checked the memory limit using ini_get(«memory_limit»); function. Then, we used ini_set(«memory_limit», «»); .
The later function takes two parameters, as shown formerly. You can set your desired memory limit in the function.
Current Memory:128M Updates Memory:256M
Set Memory Limit Using Php.ini File
First, you need to determine where your current php is installed root to the PHP directory on your machine .
Once you can locate the php.ini file, search memory_limit. It will show you the max memory a PHP script can consume, which by default must be 128MB .
After that, you have to update it to the desired memory limit. For example: memory_limit = 256M .
On the contrary, you can change it using .htacsess file in the root directory or using C-Panel if you are using C-Panel.
Change memory limit in the php.ini file:
We have accessed our php.ini file and modified the memory limit in the example above.
It is worth noting that this method applies to all of your php scripts.
You might not want to change it for all the files. Therefore, we recommend using the former method.
So that, all you need to do is define the ini_set(«memory_limit», «»); function in your scripts.
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.