Chrisissorry
Chrisissorry

Reputation: 1514

Heroku Memory Limit PHP

This article on Heroku states that 128MB should be enough for most PHP applications. I perceive this as a bold statement looking at composer which increases memory limit to 1.5GB when run.

Specifically, we run into problems when generating PDFs or handling large database exports. I guess I am missing out on some best practice here. How should we tackle memory-intensive tasks on Heroku?

One way to do achieve this would probably be to write a .user.ini to increase the memory limit. But I guess this is considered bad practice.

Upvotes: 0

Views: 2683

Answers (2)

Yayo Rivers
Yayo Rivers

Reputation: 1

To add up on @xavriley's answer, in case you don't have a Procfile because you just run your deploys with git push heroku master, you'll need to create one in your root directory.

Create a file named 'Procfile' containing this:

web: vendor/bin/heroku-php-apache2 -F fpm_custom.conf

Where fpm_custom.conf file exists in the same path as the Procfile. fpm_custom.conf file should contain the memory_size desired:

php_value[memory_limit] = 256M

Once you have created this files, commit, push, and run again git push heroku master, the deploy logs should show the new Procfile running.

Upvotes: 0

xavriley
xavriley

Reputation: 611

(disclosure - I work on the Heroku support team)

Heroku is a platform that handles a diverse range of applications - they need a balance between maximum efficiency (for apps leveraging a lot of concurrency) and enough headroom for common processing tasks. This meant that 128Mb was picked based on the experiences of the PHP team there. (see https://devcenter.heroku.com/articles/php-concurrency#determining-a-suitable-memory-limit)

Your example about composer using 1.5GB is interesting, but it's worth bearing in mind that tasks like dependency resolution and running build steps (which Composer handles) have quite a different profile from serving web requests. If you are doing something intensive like processing PDFs, ideally this should be handed off to a background process to keep the request as light as possible. Any background queueing system will work for this, but here's one example https://devcenter.heroku.com/articles/php-workers

That said, one size definitely does not fit all and it's totally possible to change from the defaults if necessary.

There are a number of options for doing this, most of which are described here: https://devcenter.heroku.com/articles/php-concurrency#tuning-concurrency-using-memory_limit

A .user.ini file is one option, however this can take effect on a per-directory basis which may cause some confusion when working with teams.

You can also configure this at either the PHP-FPM level using a custom config file with contents like so:

php_value[memory_limit] = 64M

For these settings to take effect, you need to use the -F option in your Procfile command so the config gets loaded:

web: vendor/bin/heroku-php-apache2 -F fpm_custom.conf

There are similar config options for Nginx and HHVM described in the referenced article.

Upvotes: 1

Related Questions