Php memory limit для чего

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.

Читайте также:  Php preg replace callback примеры

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.

Читайте также:  Mod and div in python

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 !

Источник

PHP Memory Limit and Why Do You Need Them?

php-memory-limit

The PHP memory limit is the maximum server memory each PHP script can extend. Per the PHP documentation: “This sets the memory in bytes that a script is allowed to designate. This makes a difference in preventing ineffectively composed scripts from eating up all accessible memory on a server.”

The memory limit setting fixes a memory size for executing a script. It prevents ineffectively coded scripts on the server that designate superfluous space, thus sparing server memory during large-scale utilization of the application.

What is the PHP Memory Limit?

The PHP memory_limit is per-script, just as a standard memory limit that is enough for any web application. A lower setting of 128M. If PHP scripts attempt to utilize more than 128M, those scripts would presently return memory limit surpassed errors.

Set the memory limit set by executing the phpinfo() function to see the memory constrain and plan your server setting according to the value. The server administrator can set a memory limit from -1—no memory assignment to any measure.

PHP scripts are tiny. Every individual script should only take up a small amount of RAM, unless you’ve got an unusual application that needs more resources in one script than most programs will ever take in their entirety. These kinds of plugins certainly exist, but they’re uncommon.

Why Is There a Memory Limit?

Increasing the memory of each process can decrease the number of concurrent processes that can run. PHP processes are critical to the maximum number of page requests you’ll serve at any given time.

When PHP benchmarks processes get overloaded, the memory limit, servers have to force stop before they completely crash, or all your distributed resources for the month get consumed.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

How Does PHP Memory Limit Relate to Performance?

Say 3 PHP scripts are utilizing 100 MB of memory each at the same time. That would add up to 300 MB of memory utilization, and a PHP limit of 128 MB wouldn’t be enough. The RAM depends on the accessibility of the data required to run a preparation or script. So if you know you’ll require more, 256 MB should cover the heaviest plugins.

Читайте также:  Готовый код питона калькулятор

Be careful when using plugins that need additional memory. Some web hosts set the PHP memory limit exceptionally low, so that can cause the mistake to appear even when you’re not installing a power-hogging application.

How to Increase the PHP Memory Limit

If your website’s needs necessitate increasing the memory limit, to complete a process, for instance, you’d need to create a php.ini (phprc) file and increase the limit there.

But Cloudways users don’t need to edit the PHP. ini file and wp-config file. They can just change the value from within the application settings using the Cloudways Platform.

cloudways-php-memory-limit

The PHP Memory Limit with Cloudways

Every site is built to serve a different purpose. You can vary the PHP memory limit you need based on this and the hosting environment you’re using. Once you get a baseline on your traffic volumes and the memory limit your application is using, you can plan for scalability in the future.

Cloudways does not limit you to a fixed memory limit size. Just like with PHP workers, we let the user increase the memory limit according to their needs.

PHP memory limit for a high-traffic ecommerce site:

Learning the Harp is a high-traffic ecommerce store accessed from around the world. Currently, it uses 6000 MBs of PHP memory to handle the traffic and requests.

When your website is hosted on one server, each client who visits it sends requests to that same server. This implies that when your website experiences high levels of traffic, the time it takes to handle each request increases, slowing down stack times for all of them. An increased PHP memory limit helps servers to handle requests more efficiently.

PHP memory limit for a high-traffic LMS site:

Pittsburgh Fresh is an LMS based in Pittsburgh, PA, that focuses on delivering fresh, healthy and convenient meals. Managed on Cloudways servers, the LMS sees exceptionally high traffic and uses about 2048 MBs of PHP memory limit to handle this. When choosing an hosted learning management system , it is important to make sure that the provider offers a sufficient PHP memory limit to meet the requirements of your LMS

Most LMS sites are built using heavy plugins. A poorly coded plugin or slow-loading theme can impact your site’s performance that results in extensive load time and affect overall user experience. A higher memory limit could help mitigate these problems.

PHP memory limit for a high-traffic multisite:

Current Debate is a top-ranked website on Google for US business news. The readers include local businesses and national franchise brands. A highly visited multisite, Current Debate uses around 16GB of PHP memory at max.

With multisite, one website has access to all the resources. To optimize performance on both the front and back ends, it might need to increase its memory limit.

Conclusion

In general, websites with essentially static content don’t require a large PHP memory limit. 128 – 256 PHP memory limit is a great starting point for bigger websites with more energetic functionality. That said, your memory limit should account for your website’s set of themes, plugins, database queries, and cached-to-uncached proportion.

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

Источник

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