How to monitor RAM for tuning pm.max_children

How to monitor RAM usage:

  1. free -h:

    • This command shows your system’s total, used, and free memory in a human-readable format.
    • Key metrics:
      • total: Total RAM.
      • used: RAM currently in use.
      • free: Unused RAM.
      • buff/cache: RAM used for file system buffers and page cache. This is good; Linux uses free RAM for this and frees it when applications need it.
      • available: The most important metric. This estimates how much memory is available for starting new applications without swapping.
    • Run it before and after: Run free -h before you increase max_children and then after your server has been running for a while under typical load with the new settings. Compare the available memory.
  2. htop (recommended if installed):

    • htop (you might need to sudo apt install htop) provides a real-time, interactive view of processes and resource usage, including a visual bar for memory usage.
    • It’s excellent for seeing how much RAM each PHP-FPM process (php-fpm: pool ... processes) is consuming. You can estimate the memory per process and multiply by your new max_children to get a rough idea of the total.
  3. top:

    • Less user-friendly than htop, but still effective.
    • Look at the %MEM column to see memory usage per process.

Practical Steps for Increasing pm.max_children and Monitoring RAM:

  1. Determine Current PHP Process Memory Usage:
    • Run htop (or top).
    • Find a few php-fpm processes from the kartscode pool. Look at their RES (Resident Set Size) or VIRT (Virtual Memory Size) columns in top/htop. This is the actual memory they are consuming. Let’s say, on average, a PHP-FPM process uses 50MB.
  2. Calculate Potential Max Memory:
    • Current max_children = 16. At 50MB/process, that’s 16 * 50MB = 800MB for PHP processes.
    • If you increase max_children to 30, that would be 30 * 50MB = 1500MB (1.5GB).
  3. Check free -h: Look at your available memory. If you only have 500MB available, increasing to 30 processes (requiring 1.5GB) will cause severe swapping.
  4. Increment Gradually: Instead of a big jump, increase pm.max_children in small steps (e.g., from 16 to 20, then 20 to 24).
  5. Monitor: After each increase, let the server run under load for an hour or two, then check free -h and htop to see how memory is being utilized. Look for sustained low available memory or increasing swap usage.

Your 17002705575366718.conf settings:

</code-block></response-element>
<div _ngcontent-ng-c2080784976="" class="code-block ng-tns-c2080784976-89 ng-animate-disabled ng-trigger ng-trigger-codeBlockRevealAnimation" jslog="223238;track:impression;BardVeMetadataKey:[[&quot;r_459b5055c3a7abbe&quot;,&quot;c_22643fe4d9561dbb&quot;,null,&quot;rc_0acabc077aae8f53&quot;,null,null,&quot;en&quot;,null,1,null,null,1,0]]">
<div _ngcontent-ng-c2080784976="" class="formatted-code-block-internal-container ng-tns-c2080784976-89">
<div _ngcontent-ng-c2080784976="" class="animated-opacity ng-tns-c2080784976-89">
<pre _ngcontent-ng-c2080784976="" class="ng-tns-c2080784976-89"><code _ngcontent-ng-c2080784976="" role="text" data-test-id="code-content" class="code-container formatted ng-tns-c2080784976-89 no-decoration-radius" data-sourcepos="135:1-141:24">pm = ondemand
pm.max_children = 16
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 8