Getting the amount of free memory in PHP
This only works on Linux (and possibly on some other Unixes) :
function getFreeMem () print getFreeMem()."\n"; ?>
{
# This function returns the % of memory which is "free"
# "free" is actually defined by "The memory which is available for programs", ie (free + cached + buffers)
$meminfo = file ("/proc/meminfo");
foreach ($meminfo as $line)
{
if (preg_match ("/(.*):\s+(\d+)/", $line, $matches))
{
$memory[$matches[1]] = $matches[2];
}
}
return ($memory["MemFree"] + $memory["Buffers"] + $memory["Cached"]) / $memory["MemTotal"];
}