memory_get_peak_usage() is used to retrieve the highest memory usage of PHP (or your running script) only. If you need the overall memory usage of the entire system, following function might be helpful. If retrieves the memory usage either in percent (without the percent sign) or in bytes by returning an array with free and overall memory of your system. Tested with Windows (7) and Linux (on an Raspberry Pi 2):
<?php
// Returns used memory (either in percent (without percent sign) or free and overall in bytes)
function getServerMemoryUsage($getPercentage=true)
{
$memoryTotal = null;
$memoryFree = null;
if (stristr(PHP_OS, "win")) {
// Get total physical memory (this is in bytes)
$cmd = "wmic ComputerSystem get TotalPhysicalMemory";
@exec($cmd, $outputTotalPhysicalMemory);
// Get free physical memory (this is in kibibytes!)
$cmd = "wmic OS get FreePhysicalMemory";
@exec($cmd, $outputFreePhysicalMemory);
if ($outputTotalPhysicalMemory && $outputFreePhysicalMemory) {
// Find total value
foreach ($outputTotalPhysicalMemory as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$memoryTotal = $line;
break;
}
}
// Find free value
foreach ($outputFreePhysicalMemory as $line) {
if ($line && preg_match("/^[0-9]+\$/", $line)) {
$memoryFree = $line;
$memoryFree *= 1024; // convert from kibibytes to bytes
break;
}
}
}
}
else
{
if (is_readable("/proc/meminfo"))
{
$stats = @file_get_contents("/proc/meminfo");
if ($stats !== false) {
// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);
// Separate values and find correct lines for total and free mem
foreach ($stats as $statLine) {
$statLineData = explode(":", trim($statLine));
//
// Extract size (TODO: It seems that (at least) the two values for total and free memory have the unit "kB" always. Is this correct?
//
// Total memory
if (count($statLineData) == 2 && trim($statLineData[0]) == "MemTotal") {
$memoryTotal = trim($statLineData[1]);
$memoryTotal = explode(" ", $memoryTotal);
$memoryTotal = $memoryTotal[0];
$memoryTotal *= 1024; // convert from kibibytes to bytes
}
// Free memory
if (count($statLineData) == 2 && trim($statLineData[0]) == "MemFree") {
$memoryFree = trim($statLineData[1]);
$memoryFree = explode(" ", $memoryFree);
$memoryFree = $memoryFree[0];
$memoryFree *= 1024; // convert from kibibytes to bytes
}
}
}
}
}
if (is_null($memoryTotal) || is_null($memoryFree)) {
return null;
} else {
if ($getPercentage) {
return (100 - ($memoryFree * 100 / $memoryTotal));
} else {
return array(
"total" => $memoryTotal,
"free" => $memoryFree,
);
}
}
}
function getNiceFileSize($bytes, $binaryPrefix=true) {
if ($binaryPrefix) {
$unit=array('B','KiB','MiB','GiB','TiB','PiB');
if ($bytes==0) return '0 ' . $unit[0];
return @round($bytes/pow(1024,($i=floor(log($bytes,1024)))),2) .' '. (isset($unit[$i]) ? $unit[$i] : 'B');
} else {
$unit=array('B','KB','MB','GB','TB','PB');
if ($bytes==0) return '0 ' . $unit[0];
return @round($bytes/pow(1000,($i=floor(log($bytes,1000)))),2) .' '. (isset($unit[$i]) ? $unit[$i] : 'B');
}
}
// Memory usage: 4.55 GiB / 23.91 GiB (19.013557664178%)
$memUsage = getServerMemoryUsage(false);
echo sprintf("Memory usage: %s / %s (%s%%)",
getNiceFileSize($memUsage["total"] - $memUsage["free"]),
getNiceFileSize($memUsage["total"]),
getServerMemoryUsage(true)
);
?>
The function getNiceFileSize() is not required. Just used to shorten size in bytes.
Note: If you need the server load (CPU usage), I wrote a nice function to get that too: https://php.net/manual/en/function.sys-getloadavg.php#118673| CARVIEW |
Select Language
HTTP/2 301
server: myracloud
date: Tue, 30 Dec 2025 10:11:22 GMT
content-type: text/html
content-length: 161
location: https://www.php.net/manual/function.memory-get-peak-usage
HTTP/2 302
server: myracloud
date: Tue, 30 Dec 2025 10:11:23 GMT
content-type: text/html; charset=utf-8
content-length: 0
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
location: https://www.php.net/manual/en/function.memory-get-peak-usage
expires: Tue, 30 Dec 2025 10:11:23 GMT
cache-control: max-age=0
HTTP/2 302
server: myracloud
date: Tue, 30 Dec 2025 10:11:23 GMT
content-type: text/html; charset=utf-8
content-length: 0
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
location: https://www.php.net/manual/en/function.memory-get-peak-usage.php
expires: Tue, 30 Dec 2025 10:11:23 GMT
cache-control: max-age=0
HTTP/2 200
server: myracloud
date: Tue, 30 Dec 2025 10:11:23 GMT
content-type: text/html; charset=utf-8
content-language: en
permissions-policy: interest-cohort=()
x-frame-options: SAMEORIGIN
link: ; rel=shorturl
last-modified: Tue, 30 Dec 2025 10:08:12 GMT
vary: accept-encoding
content-encoding: gzip
expires: Tue, 30 Dec 2025 10:11:23 GMT
cache-control: max-age=0
PHP: memory_get_peak_usage - Manual
update page now
memory_get_peak_usage
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
memory_get_peak_usage — Returns the peak of memory allocated by PHP
Description
Returns the peak of memory, in bytes, that's been allocated to your PHP script.
Parameters
Return Values
Returns the memory peak in bytes.
See Also
- memory_get_usage() - Returns the amount of memory allocated to PHP
- memory_reset_peak_usage() - Reset the peak memory usage
- memory_limit
+add a note
User Contributed Notes 1 note
stanislav dot eckert at vizson dot de ¶
8 years ago
↑ and ↓ to navigate •
Enter to select •
Esc to close • / to open
Press Enter without
selection to search using Google