I no creo que pueda acceder a la información de la memoria del servidor host sin una extensión especial PHP escrita. La biblioteca principal de PHP no permite (tal vez por razones de seguridad) acceder a la información de la memoria extendida.
Sin embargo, si su script tiene acceso al /proc/meminfo
, entonces puede consultar ese archivo especial y obtener la información que necesita. En Windows (aunque no lo haya solicitado) podemos usar la extensión PHP com_dotnet
para consultar el marco de trabajo de Windows a través de COM.
A continuación puede encontrar mi getSystemMemoryInfo
que devuelve esa información para usted, sin importar si ejecuta la secuencia de comandos en un servidor Linux/Windows. El wmiWBemLocatorQuery
es solo una función de ayuda.
function wmiWBemLocatorQuery($query) {
if (class_exists('\\COM')) {
try {
$WbemLocator = new \COM("WbemScripting.SWbemLocator");
$WbemServices = $WbemLocator->ConnectServer('127.0.0.1', 'root\CIMV2');
$WbemServices->Security_->ImpersonationLevel = 3;
// use wbemtest tool to query all classes for namespace root\cimv2
return $WbemServices->ExecQuery($query);
} catch (\com_exception $e) {
echo $e->getMessage();
}
} elseif (! extension_loaded('com_dotnet'))
trigger_error('It seems that the COM is not enabled in your php.ini', E_USER_WARNING);
else {
$err = error_get_last();
trigger_error($err['message'], E_USER_WARNING);
}
return false;
}
// _dir_in_allowed_path this is your function to detect if a file is withing the allowed path (see the open_basedir PHP directive)
function getSystemMemoryInfo($output_key = '') {
$keys = array('MemTotal', 'MemFree', 'MemAvailable', 'SwapTotal', 'SwapFree');
$result = array();
try {
// LINUX
if (! isWin()) {
$proc_dir = '/proc/';
$data = _dir_in_allowed_path($proc_dir) ? @file($proc_dir . 'meminfo') : false;
if (is_array($data))
foreach ($data as $d) {
if (0 == strlen(trim($d)))
continue;
$d = preg_split('/:/', $d);
$key = trim($d[0]);
if (! in_array($key, $keys))
continue;
$value = 1000 * floatval(trim(str_replace(' kB', '', $d[1])));
$result[$key] = $value;
}
} else // WINDOWS
{
$wmi_found = false;
if ($wmi_query = wmiWBemLocatorQuery(
"SELECT FreePhysicalMemory,FreeVirtualMemory,TotalSwapSpaceSize,TotalVirtualMemorySize,TotalVisibleMemorySize FROM Win32_OperatingSystem")) {
foreach ($wmi_query as $r) {
$result['MemFree'] = $r->FreePhysicalMemory * 1024;
$result['MemAvailable'] = $r->FreeVirtualMemory * 1024;
$result['SwapFree'] = $r->TotalSwapSpaceSize * 1024;
$result['SwapTotal'] = $r->TotalVirtualMemorySize * 1024;
$result['MemTotal'] = $r->TotalVisibleMemorySize * 1024;
$wmi_found = true;
}
}
// TODO a backup implementation using the $_SERVER array
}
} catch (Exception $e) {
echo $e->getMessage();
}
return empty($output_key) || ! isset($result[$output_key]) ? $result : $result[$output_key];
}
ejemplo en un sistema de 8 GB de RAM
print_r(getSystemMemoryInfo());
salida
Array
(
[MemTotal] => 8102684000
[MemFree] => 2894508000
[MemAvailable] => 4569396000
[SwapTotal] => 4194300000
[SwapFree] => 4194300000
)
Si se quiere entender lo que cada campo representa a continuación read more.
para Windows hay una extensión PECL: http://php.net/manual/en/function.win32-ps-stat- mem.php – Gordon
Cosas geniales para chicos geniales: https://github.com/BitOne/php-meminfo – Sliq