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.");
}