"PHP Memory Limit" — How to Increase Your RAM

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. Convenient, huh?

For example, in Ubuntu:

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

On different versions of Linux or BSD, these files may be in different locations but are generally easy to find. Thanks, Google!

Note: On shared hosts, you may not have access to these files. Sorry, folks. You can try to get in touch with your host, but they usually don’t want someone eating up all the RAM on the server because it is shared with other folks.

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.

Bonus Step: 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:

It’s Been Real…But Goodbye

Well, there you have it: You are now a PHP memory changing expert. It’s worth noting that you should poke around your php.ini file to see what’s in there. At some point, you will probably want to change the maximum file size for uploads (upload_max_filesize & post_max_size), but you get the idea. See ya!

Ryan Frankel

Questions or Comments? Ask Ryan!

Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.