There are several discussions complaining high memory usage of PHP-FPM
, without any practical approach to overcome. As I explored the problem with numerous tests, the problem is related to the age of PHP-FPM
age.
Imagine we have pm.max_children
= 10, and have a PHP script consuming 10M memory. One expects needing 100MB of MB. This is correct for the first 10 processes. After processing 20 PHP scripts, the system memory usage is 200MB, which means that the memory consumed during the first 10 processes have not been freed.
This will increase the memory usage until reaching pm.max_requests
when PHP-FPM
processes are reset.
NOTE: This was just a roughly simplified example for clarification, and not actual behavior. In real world, this happens sometimes. Normally, at the end of a PHP script, the memory consumed by PHP will be automatically freed. In PHP-FPM, this memory sometimes
(I don't know when) will not be freed.
This means that for processing similar scripts (using 10MB of memory) with
pm.max_children = 10
pm.max_requests = 100
You do not need 100MB or 1000MB of memory; but something in between. In other words, footprints of some PHP previous processes
remain on the PHP-FPM
children.
In agreement with this theory, the high memory usage of PHP-FPM
will be overcome by reducing pm.max_requests
, but it is not the ultimate solution, and we need to find an approach to free the effects of previous PHP processes on the PHP-FPM
children. Any idea?
Without knowing exactly what kind of app you're running, it's hard for us to say what you should do. But, have considered using the ondemand process manager? It'll kill off processes after N time, thus keeping your memory freer, than the dynamic or static you're probably using.