I know that there are a lot of related subjects, but I did not found my answer.

I have a dedicated server with 32Go of RAM (but I want php-fpm to use only 12Go max), and I have a php-fpm (7.2.13) running along side an apache server.

My problem is that each php-fpm process is consuming too much memory... I mesured it with

ps -ylC php-fpm --sort:rss

And the output was 269 process, each of them with 121Mo in the RSS column, and a total RAM usage on the dedicated server of 3Go.

I also made some php profiling with "memory_get_peak_usage()" php function, and each of my script use an average of 6Mo (peak usage !) and a maximum peak usage of 50Mo (but it is a very specific case). After this profiling, I tried to set the "memory_limit" to 10Mo, the specific case was not working, so I set it to 50Mo and all was ok (normal behaviour).

My two questions are:

  • As my php scripts does not exceed a peak usage of 50Mo, and as I set a memory_limit of 50Mo, why EACH of my php-fpm process are 121Mo big ?

  • How is it possible to have 269 processes running with each of them 121Mo big ? The total amout of memory would be 269*121 = 32Go (and when I made this profiling, the total amount of RAM used is the dedicated server was only 3Go ...)

Thanks a lot for your help !


First question: the php-fpm processes need memory for the php interpreter itself; the 50Mb limit limits the amount of RAM the script can consume, in addition to what the interpreter needs no matter what.

Second question: your processes probably shared quite a bit of memory (definitely all code pages that were loaded from the same binary/library).

| improve this answer | |
  • Thank you for your answer ! Is there a way to see the memory really used by a process, without the shared memory ? In order to be able to tune my php-fpm max-children correctly – HawkLiking Jun 3 '19 at 20:36
  • And I don't have Opcache enabled on my server, so I don't understand what can be this shared memory, if this is not my code/lib opcode in cache – HawkLiking Jun 3 '19 at 21:21
  • 1
    The shared memory is also "really used by the process" -- it's just used by more than one process. Memory management in a modern operating system is very complex. Instead of trying to work out how much RAM each process needs individually, I suggest either using cgroups to limit the total amount of RAM all php-fpm processes together can consume, or at least using cgroups to track their total usage and adjust their number based on that. – András Korn Jun 4 '19 at 6:19
  • My php-fpm is in a docker, so I just have to set the "mem_limit" of this docker to 12Go and it will be fine (and disable swap). But I wanted to know exactly how many processes I could fit in these 12Go. I mean, if in the 121Mo , 75Mo are "shared memory" used by all processes, I can consider that each process use 46Mo, and find out the right "max_children" for 12Go.. but I don't know how to find this number. I tried to look at cgroups but I'm not sure to understand how I must do it ? – HawkLiking Jun 4 '19 at 9:14
  • This morning on my docker, I had 2 active processes, and 38 idle (so 40 total processes). The memory taken by my docker was 668Mo. If I take 500Mo / 40 processes, it gives ~13Mo by process. Does that sound correct to you ? – HawkLiking Jun 4 '19 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.