When you create a new PHP project, RAM is automatically allotted. The majority of uses for this memory are in general. However, there are some situations where errors like these appear, for instance, when you try to load large photos on a page with high graphics.
“Fatal error: Allowed memory size of xxxxxx bytes exhausted”
“filename.jpg exceeds the maximum upload size for this site.”
Raising the application’s RAM limit and getting in touch with the hosting company are the best ways to fix this error. However, there are methods, such as utilizing php.ini files,.htacess files, etc., to raise the memory limit of the entire website or a specific script without the assistance of a specialist. We’ll go over several methods for raising the memory limit in this blog post, along with the advantages of doing so for your PHP application.
What is the RAM limit for PHP?
PHP memory limit is the amount of memory that each PHP script is given. It’s the same as the maximum amount of storage that a specific operation can take up. In certain situations, PHP scripts’ memory limit can be helpful. For instance, there may be instances where some shoddy code attempts to consume the entire stack’s memory.
The memory limit on most WordPress websites is 32M, but in many situations, you might need additional memory. For instance, you should increase the RAM limit of your script if you are performing intensive activities like frequent database queries and intensive picture processing.
Techniques to Think About
We will discuss several methods in this blog post to raise the RAM limits of your PHP scripts and applications. These are the best practices that most developers follow when dealing with memory limit concerns, albeit they are not the only approaches to boost the memory limitations of your PHP script.
Additionally, modifying an application’s RAM limit may occasionally result in issues, so you should constantly backup your system’s data.
You should always speak with your website’s server providers before attempting to increase memory. By applying their best methods, they can assist you in raising the memory limit.
First tactic: Modify the PHP.ini file
Every time a PHP application starts, the php.ini file is processed. Its purpose is to manage the different PHP script settings, such as the maximum upload size, memory limit, timeout limit, etc.
You can adjust the following variables to raise the memory limit. However, take note that these variables are case-sensitive and that in order for changes to take effect, you must restart the server.
memory_limit = 256M
upload_max_filesize = 12M
post_max_size = 13M
file_uploads = On
max_execution_time = 180
The maximum amount of time that the screen can be displayed is known as the “max execution time,” which also relates to the PHP script’s timeout.
Method2: Modify the HTAccess file
The.htacess file’s name begins with a dot because it is a secret file. You must alter the.htacess file to raise the memory limit if you are using shared hosting or if you are unable to access the php.ini file for whatever reason.
This.htacess file can be used in many different situations. To raise the memory limit, you must add the following lines to this file.
php_value memory_limit 256M
php_value upload_max_filesize 12M
php_value post_max_size 13M
Method 3: Boost using the ini_set() Function
To set an attribute’s value only within the context of the script, use the ini_set() function. Because it only sets the value specifically for the script and prevents poorly written scripts from using up all of the server’s memory, it is regarded as the safest of the aforementioned methods.
You can just do to use this function to raise the memory limit.
The 512 MB memory limit will be established using the aforementioned function. Furthermore, the ini_set() function should only be used to temporarily set the value of a variable; upon restarting the script, the original values from the php.ini file will be utilized.
This memory limit issue is one of several frequent ones. We’ve covered a variety of tactics for raising your script’s memory limit in this blog. It’s important to remember that increasing the memory limit of your PHP script should only be done as a last resort because it might have a significant impact on your website.
It is imperative that you inquire about the activities and challenges that consumers are encountering on your website after it is operational for real users. It will also aid in boosting client retention and engagement. The greatest contemporary option for tracking your PHP application is Scout APM. Support for numerous languages, including PHP, Ruby, Python, and others, is included. In comparison to market competitors, the pricing is relatively reasonable and you will receive 24/7 customer assistance.