tags:

views:

438

answers:

4

Hi,

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).

+2  A: 

it would still be limited by memory_limit...

no way that i know of to do this cross-platform, php-only way.

n00b32
should this be a comment?
Mike B
Should not be a comment because the original question intent is unclear. If the intent of the original poster is to modify script behavior based on available system RAM, then this person should be aware that php.ini defines resource limits like memory_limit. And in that case, this is a decent answer (although could be elaborated...). If that's not the intent and they just want to display RAM stats, well, then answers like this are just a side effect of not posting your intent with the question.
indiv
@indiv The question seems extremely straight-forward to me. How does one get server ram via php with no shell commands? Why speculate further with how this is intended to be used if the asker does not wish it? With your logic I could answer with something about PHP's garbage collection process and expect it to somehow be relevant. There would no be no end to the amount of answers to a seemingly straight-forward question. Intent is not a non-issue here until the asker makes it one.
Mike B
maybe seems that to you but it still could be used to see for example how big buffers can be used etc etc -.-'''but i have edited the anwser to be err moar relevant ?
n00b32
maybe i'm wrong, but the OP asks specifically for: "linux distro" so i don't think cross-platform is relevant here
jcinacio
u r rite ;) but the quest allready anwsered so ..
n00b32
A: 

I don't remember having ever seen such a function -- its kind of out the scope of what PHP is made for, actually.

Even if there was such a functionnality, it would probably be implemented in a way that would be specific to the underlying operating system, and wouldn't probably work on both Linux and windows (see sys_getloadavg for an example of that kind of thing)

Pascal MARTIN
+6  A: 

If you know this code will only be running under Linux, you can use the special /proc/meminfo file to get information about the system's virtual memory subsystem. The file has a form like this:

MemTotal:       255908 kB
MemFree:         69936 kB
Buffers:         15812 kB
Cached:         115124 kB
SwapCached:          0 kB
Active:          92700 kB
Inactive:        63792 kB
...

That first line, MemTotal: ..., contains the amount of physical RAM in the machine, minus the space reserved by the kernel for its own use. It's the best way I know of to get a simple report of the usable memory on a Linux system. You should be able to extract it via something like the following code:

<?php $fh = fopen('/proc/meminfo.txt');
$mem = 0;
while ($line = fgets($fh)) {
  $pieces = array();
  if ($preg_match('^MemTotal:\s+(\d+)\skB$', $line, &$pieces) {
    $mem = $pieces[1];
    break;
  }
}

echo "$mem kB RAM found"; ?>

(Please note: this code may require some tweaking for your environment.)

rcoder
upvoted for the meminfo, with 3 gotchas: - its NOT '/proc/meminfo.txt', but '/proc/meminfo' - you should match 'MemFree', and not 'MemTotal'. - and you forgot fclose() ;)
jcinacio
You're right that the `.txt` suffix is wrong, but I did mean to use `MemTotal`, not `MemFree` -- perhaps I misunderstood the original question, though. Re-reading it, I see the word "available", though I'm honestly not sure how knowing the *available* RAM on a server helps all that much, given the nature of modern VM and cache subsystems.Also, the omission of `fclose()` shouldn't really be a factor for short-lived PHP scripts, correct? It's hard to leak file descriptors when your GC can finalize open handles...
rcoder
+1  A: 

Using '/proc/meminfo' and getting everything into an array is simple:

<?php

function getSystemMemInfo() 
{       
    $data = explode("\n", file_get_contents("/proc/meminfo"));
    $meminfo = array();
    foreach ($data as $line) {
     list($key, $val) = explode(":", $line);
     $meminfo[$key] = trim($val);
    }
    return $meminfo;
}

?>

var_dump( getSystemMemInfo() );

array(43) {
  ["MemTotal"]=>
  string(10) "2060700 kB"
  ["MemFree"]=>
  string(9) "277344 kB"
  ["Buffers"]=>
  string(8) "92200 kB"
  ["Cached"]=>
  string(9) "650544 kB"
  ["SwapCached"]=>
  string(8) "73592 kB"
  ["Active"]=>
  string(9) "995988 kB"
  ...
jcinacio