get server ram with php

Viewed 48970

Is there a way to know the avaliable ram in a server (linux distro) with php (widthout using linux commands)?

edit: sorry, the objective is to be aware of the ram available in the server / virtual machine, for the particular server (even if that memory is shared).

9 Answers

Linux commands can be run using the exec function in PHP. This is efficient and will do the job(if objective is to get the memory).

Try the following code:

<?php
  exec("free -mtl", $output);
  print_r($output);
?>

Small and tidy function to get all of its values associated to their keys.

$contents = file_get_contents('/proc/meminfo');
preg_match_all('/(\w+):\s+(\d+)\s/', $contents, $matches);
$info = array_combine($matches[1], $matches[2]);

// $info['MemTotal'] = "2047442"

exec("grep MemTotal /proc/meminfo", $aryMem); $aryMem[0] has your total ram minus kernel usage.

Related