How to Increase Your PHP Memory Limit?

Sometimes while in development or production, we find that our software uses more memory than we ever expected. To keep things under control, PHP has default memory limits to avoid some runaway program from crashing your box.

Luckily, it is extremely easy to change how much RAM we allocate toward a single PHP script. We can even let PHP use an infinite (or all that’s available, to be more precise) amount of memory.

Really, there are only two simple steps to getting this done so you can move on with your life.

1. Find Your php.ini

The first thing to remember is that there are usually two versions of php.ini on Linux. This is because you can have a different version for command line scripts and for web servers.

For example, in Ubuntu:

  • Command line: /etc/php5/cli/php.ini
  • Apache: /etc/php5/apache2/php.ini

Note: On shared hosts, you may not have access to these files. So, you need to get in touch with your hosting provider and ask them to increase it for you.

2. Edit Your “memory_limit”

Once you find the php.ini file, you want to open it in your favorite text editor (vim/nano/etc.).

The line you are looking to edit is called the memory_limit. It will look similar to the following:

In this example, the memory limit is set to 64MB. If you wanted to change it to 128MB or 32MB or whatever, you just need to change this line. Once you have this line set appropriately, save and exit your php.ini file.

For web servers (!): If you’re changing a web server’s php.ini you must restart the web server. For example on Ubuntu with Apache, you would need to run:

After that, your memory limit should change accordingly.

Setting the PHP “memory_limit -1”

If you are feeling extra frisky and know what you are doing, you can set the memory limit to use all available RAM. This is “dangerous” in that, if your script runs out of control, it will crash your machine. Generally, this wouldn’t be recommended for a production environment, but, hey, it’s your life. Don’t let the man hold you down.

To accomplish this just set the memory limit to -1 like this:

Latest posts by Steve (see all)