How to Increase PHP Memory Limits

Published on by

How to Increase PHP Memory Limits image

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: 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 24x7 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!

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Visit Paragraph
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel image

DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel

Read article
Sort Elements with the Alpine.js Sort Plugin image

Sort Elements with the Alpine.js Sort Plugin

Read article
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article