Memory is physical and swap, so you need to determine the criteria you wish to compare when you indicate values like low, medium, high. I graph my server memory and keep my eye on a constant System, currently around 40%, and a very low swap file.
If I were to use an if() statement to determine tasks I suppose I would use the swap file as the deciding factor: low < 5%; high > 70%.
You will find memory usage in the file meminfo in the proc directory.
Sample:
Code:
MemTotal: 451100 kB
MemFree: 21956 kB
Buffers: 76644 kB
Cached: 170976 kB
SwapCached: 19332 kB
Active: 244424 kB
Inactive: 127024 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 451100 kB
LowFree: 21956 kB
SwapTotal: 2048276 kB
SwapFree: 1962216 kB
Dirty: 1216 kB
Writeback: 0 kB
Mapped: 139048 kB
Slab: 50756 kB
Committed_AS: 443792 kB
PageTables: 2240 kB
VmallocTotal: 565240 kB
VmallocUsed: 3644 kB
VmallocChunk: 561348 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 4096 kB
and the size of the swap files in the file swaps in the proc directory.
Sample:
Code:
Filename Type Size Used Priority
/dev/hda3 partition 2048276 86060 -1
Filename Type Size Used Priority
/dev/hda3 partition 2048276 86060 -1
Filename Type Size Used Priority
/dev/hda3 partition 2048276 86060 -1
Filename Type Size Used Priority
/dev/hda3 partition 2048276 86060 -1
Filename Type Size Used Priority
/dev/hda3 partition 2048276 86060 -1
Sample Code function to yield Physical memory (System, Buffered, & Cached) and Swap (Total & Available):
PHP Code:
<?
function memory () {
if ($fd = fopen('/proc/meminfo', 'r')) {
$results['ram'] = array();
$results['swap'] = array();
$results['devswap'] = array();
while ($buf = fgets($fd, 4096)) {
if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['ram']['total'] = $ar_buf[1];
} else if (preg_match('/^MemFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['ram']['t_free'] = $ar_buf[1];
} else if (preg_match('/^Cached:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['ram']['cached'] = $ar_buf[1];
} else if (preg_match('/^Buffers:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['ram']['buffers'] = $ar_buf[1];
} else if (preg_match('/^SwapTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['swap']['total'] = $ar_buf[1];
} else if (preg_match('/^SwapFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
$results['swap']['free'] = $ar_buf[1];
}
}
fclose($fd);
$results['ram']['t_used'] = $results['ram']['total'] - $results['ram']['t_free'];
$results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
$results['swap']['used'] = $results['swap']['total'] - $results['swap']['free'];
$results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
// values for splitting memory usage
if (isset($results['ram']['cached']) && isset($results['ram']['buffers'])) {
$results['ram']['app'] = $results['ram']['t_used'] - $results['ram']['cached'] - $results['ram']['buffers'];
$results['ram']['app_percent'] = round(($results['ram']['app'] * 100) / $results['ram']['total']);
$results['ram']['buffers_percent'] = round(($results['ram']['buffers'] * 100) / $results['ram']['total']);
$results['ram']['cached_percent'] = round(($results['ram']['cached'] * 100) / $results['ram']['total']);
}
$swaps = file ('/proc/swaps');
for ($i = 1; $i < (sizeof($swaps)); $i++) {
$ar_buf = preg_split('/\s+/', $swaps[$i], 6);
$results['devswap'][$i - 1] = array();
$results['devswap'][$i - 1]['dev'] = $ar_buf[0];
$results['devswap'][$i - 1]['total'] = $ar_buf[2];
$results['devswap'][$i - 1]['used'] = $ar_buf[3];
$results['devswap'][$i - 1]['free'] = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
$results['devswap'][$i - 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
}
} else {
$results['ram'] = array();
$results['swap'] = array();
$results['devswap'] = array();
}
return $results;
}
?>