bref icon indicating copy to clipboard operation
bref copied to clipboard

Add documentation about PHP memory limit

Open Nyholm opened this issue 4 years ago • 11 comments

When configure Lambda's memory I naively thought that is the amount of memory PHP will use. I forgot about memory_limit in php.ini.

Correct me if Im wrong. But PHP-FPM will be running on a "warm" Lambda. Say that I've configured it with 1792Mb of memory. For every new request I get 1792Mb. Right, So why not specify memory_limit: 1792M?

Same with using console commands. Right?

Nyholm avatar Aug 19 '19 06:08 Nyholm

Yes that's correct.

The hard thing here is that we should set PHP's memory limit to the lambda's memory limit. And it varies based on the configuration of the user.

mnapoli avatar Aug 19 '19 08:08 mnapoli

Can that be injected during serverless deploy? Or would setting PHP's memory_limit directive to -1 in Bref's base php.ini do the trick?

nealio82 avatar Aug 19 '19 13:08 nealio82

Can that be injected during serverless deploy?

Maybe, I haven't looked into that so I have no idea if it would be easy to do.

Setting -1 as the limit might be risky: PHP will never run the "garbage collector" and you might end up with aborted lambdas (because they hit the limit) even though there was no reason to. I'm not sure if what I say makes sense, and honestly I'm just speculating.

mnapoli avatar Aug 19 '19 13:08 mnapoli

To inject the value during deploy is more "magic". It is also hard to figure out if a default value was used or if the user intentionally set that value.

I think this is a feature to the documentation.

Nyholm avatar Aug 19 '19 13:08 Nyholm

Bah, I think both cases have pitfalls. You're right about it being too magic and should be exposed to the user, but on the other hand as a user I think I'd want to set the value once in serverless.yaml for everything and not have to worry about remembering to set memory limits in other places.

If it is actually possible to inject the value, maybe we could have an auto mode where Bref figures out what it thinks should be the limit based on your Lamda's memory, and you can choose to override it if you want to?

nealio82 avatar Aug 19 '19 14:08 nealio82

Can we we have it both ways? Can it be set once in serverless.yaml and then override it during execution (if needed)

franksierra avatar Aug 19 '19 14:08 franksierra

Actually garbage collection should still kick in even with the memory_limit=-1 as it's only relying on the number of objects which have been fallen out of scope reach a treshold.

See also https://www.php.net/manual/de/features.gc.performance-considerations.php#features.gc.performance-considerations.slowdowns

jenschude avatar Nov 15 '19 14:11 jenschude

The php.ini does support reading environment variables so this works:

memory_limit=${AWS_LAMBDA_FUNCTION_MEMORY_SIZE}M

Although this would need to be tested in the docker images.

Nemo64 avatar Aug 13 '20 19:08 Nemo64

Oh this is awesome!

mnapoli avatar Aug 14 '20 12:08 mnapoli

Does that memory_limit setting include the memory for fpm etc? If not, we might still be out of luck because we only have our max memory minus the overhead (fpm, etc) available.

georgeboot avatar Dec 02 '20 18:12 georgeboot

@georgeboot you are correct, there needs to be a margin for those other processes.

mnapoli avatar Dec 02 '20 20:12 mnapoli