Display memory usage in Drupal/PHP

    0

    0

    Drupal 8/9

    The function calculates the memory usage in bytes for the given value of 1024, 1048576, or 1 megabyte. If the value is less than 1024 bytes, the memory usage is rounded down to the nearest whole number and logged as a warning. If the value is greater than or equal to 1024 bytes, the memory usage is rounded down to the nearest whole number and logged as a warning. Otherwise, the memory usage is rounded up to the nearest whole number and logged as a warning.

    function echo_memory_usage() {
      $mem_usage = memory_get_usage(TRUE);
    
      if ($mem_usage < 1024) {
        $restult = $mem_usage . " bytes";
      }
      elseif ($mem_usage < 1048576) {
        $restult = round($mem_usage / 1024, 2) . " kilobytes";
      }
      else {
        $restult = round($mem_usage / 1048576, 2) . " megabytes";
      }
    
      Drupal::logger("memory")->warning("Memory usege: $restult.");
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.