/**
* Calculates current PHP memory usage relative to current ini memory_limit.
*
* @return array Memory stats containing current bytes, limit bytes, and usage percentage.
*/
function devdeck_get_php_memory_metrics() {
$memory_limit_raw = ini_get('memory_limit');
$usage_bytes = memory_get_usage(true); // Total memory allocated from system
if ($memory_limit_raw === '-1') {
return [
'used_bytes' => $usage_bytes,
'limit_bytes' => -1,
'memory_usage_pct' => 0,
];
}
$limit_bytes = devdeck_convert_hr_to_bytes($memory_limit_raw);
$usage_pct = ($limit_bytes > 0) ? round(($usage_bytes / $limit_bytes) * 100, 2) : 0;
return [
'used_bytes' => $usage_bytes,
'limit_bytes' => $limit_bytes,
'memory_usage_pct' => $usage_pct,
];
}
/**
* Helper to convert shorthand memory notation (e.g. '256M', '1G') to bytes.
*/
function devdeck_convert_hr_to_bytes($value) {
$value = trim($value);
$unit = strtolower(substr($value, -1));
$bytes = (int) $value;
switch ($unit) {
case 'g':
$bytes *= 1024;
// fallthrough
case 'm':
$bytes *= 1024;
// fallthrough
case 'k':
$bytes *= 1024;
break;
}
return $bytes;
}
MkM
Skip to content
@mkmcst
Profile
Registered: 8 years, 9 months ago
follow me on the all new friends network make new friends at mkmcst.net
Website: https://mkmcst.com
Forums
Last Activity: 3 years, 4 months ago
Topics Started: 3
Replies Created: 0
Forum Role: Keymaster