Setting memory limit in php ini

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.

Читайте также:  Python dataframe show columns

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 !

Источник

Increase PHP Memory Allowance Using ini_set()

On rare occasion, I need to up the memory allowed for PHP. Usually it’s when I deal with large files and don’t necessarily care about optimizing the file reading process but simply getting the file opened. There’s an easy way to increase to amount of memory allowed to PHP right in your script:

The above code will increase the maximum amount of memory available to PHP to 16 MB. Again, the setting is only adjusted for the running script.

Recent Features

CSS vs. JS Animation: Which is Faster?

CSS vs. JS Animation: Which is Faster?

5 HTML5 APIs You Didn’t Know Existed

5 HTML5 APIs You Didn’t Know Existed

Incredible Demos

Create a Twitter AJAX Button with MooTools, jQuery, or Dojo

Create a Twitter AJAX Button with MooTools, jQuery, or Dojo

Optimize Your Links For Print Using CSS — Show The URL

Discussion

I had to do this recently for a client who had joomla (yuck) installed. I just posted this in the top of their main index.php file so it was loaded on every page. It’s really good as a quickfix if you’re using something like Gallery or Joomla along with php-cgi, since php-cgi doesnt let you use htaccess.

Thank you o-b-wan, both for this post, and the kindly remonstration given on the other post that sent me here. 🙂 Michael

This worked great David. I had to place it below some other “session_start();” code in the index2.php file of a troublesome joomla installation, but it worked just as you offered. memory_limit Local 16M; Master 8M Thanks again o-b-wan. Off to look for the next pebble…
🙂 Michael

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 122880 bytes) in /var/virtual/web/w1032/html/wp-content/plugins/wp-postratings/wp-postratings.php on line 872

Which file should I place the code in? And I obviously need to increase it past 16MB so can I just put in:

I cannot install plugins on wordpress due to a memory error. I can’t get to php.ini, which file do you know should I put ini_set(‘memory_limit’,’24M’) in? wp-config, the plugin script or all the wp php files? thanks!

I’ve got a strange behaviour on PHP 5.2.11.
The php.ini set the memory_limit to 128MB. I can adjust the limit to 64M but never above the limit of 128M. Did someone noticed a similar problem?

I tried to install wordpress 3.0.1 and i received this message : Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2966269 bytes) in /home/blidaru/public_html/wp-includes/http.php on line 1364 What i have to do?

ini_set(‘memory_limit’, ‘-1’); will be a neat solution as well. but I am still not sure of any side effects of this!

Dangerous! If you happen to have any flaw in your code, this could cause your server to become unresponsive. Setting the memory_limit to -1 (infinite) should only be done for testing purposes on non-production machines!

You really save my life.
I was trying to work on local with prestashop 1.7, and nothing works for backoffice access products…
That’s was the solution!

Life saver, as always David. Host of my dedicated saw fit to lower all max memory across their entire network for “security reasons”. Screw em, Big Daddy Walsh has my answer!

Thanks David but it didn’t work in my case – I think my host has put a limit on memory access – had an image resizing script with imagecreatetruecolor() – and script just would not go past it.

Just so you know, the administrator CAN limit your ability to increase this limit! There’s such a thing called PHP Safe Mode, and in this mode you can set function calls that will NOT be executed, no matter who calls them or from where. One of these functions can be ini_set() , which would block it completely. You can also try setting it via .htaccess, but that doesn’t always work (it hinges on ALLOW OVERRIDES being set to something other than NONE, I believe). Keep that in mind. Sometimes, the only valid solution is to change your host.

Hi David, I’m trying this right now. Somehow from php.ini it’s already 512M. In wp_config.php I also put define(‘WP_MEMORY_LIMIT’, ‘512M’); but somehow it keep getting 256M. I decided to use the ini_set(‘memory_limit’,’1024M’); in my php file to see if it has any improvement. So far it’s not breaking due to memory limit yet. So thank very much for pointing this out.

ini_set('memory_limit', '128M'); ini_set('upload_max_filesize', '128M'); ini_set('post_max_size', '128M'); ini_set('file_uploads', 'On'); ini_set('max_execution_time', '300'); ini_set('file_uploads', 'On');

Источник

Оцените статью