Web Hosting Talk







View Full Version : Getting accurate memory used


RangerOfFire
05-22-2005, 11:42 AM
I have a script at http://dandd.cubepower.net/cubestatus-1p0p2/ (download at http://dandd.cubepower.net/cubestatus-1p0p2.zip )

which does not display the mem used corretly, I am getting the free memory from /proc/meminfo, but it does not account for memory in cache. I am using the MemTotal: and MemFree: parts.

Is there another way or could I use a different part of /proc/meminfo ?

I know of the free command, but how do I use php to get the buffers and cached values?

Burhan
05-23-2005, 03:04 AM
Try this (could use some polishing):


<?php

$output = shell_exec('free');
$lines = explode("\n",$output);
$mem_info = explode("\t",$lines[1]);
$mem_info = preg_split("/[\\s]+/",$mem_info[0]);
$swap = explode("\t",$lines[3]);
$swap = preg_split("/[\\s]+/",$swap[0]);
echo "Memory Information\n";
echo "Total : ".$mem_info[1]."\n";
echo "Used : ".$mem_info[2]."\n";
echo "Free : ".$mem_info[3]."\n";
echo "Shared : ".$mem_info[4]."\n";
echo "Buffers : ".$mem_info[5]."\n";
echo "Cache : ".$mem_info[6]."\n";
echo "Swap Information\n";
echo "Total : ".$swap[1]."\n";
echo "Used : ".$swap[2]."\n";
echo "Free : ".$swap[3]."\n\n";

?>