1<?php
2
3function memory_get_usage() {
4  // If its Windows
5  // Tested on Win XP Pro SP2. Should work on Win 2003 Server too
6  // Doesn't work for 2000
7  // If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
8  if ( substr(PHP_OS,0,3) == 'WIN') {
9    $output = array();
10    exec(HTML2PS_DIR.'utils/pslist.exe -m ' . getmypid() , $output);
11
12    $resultRow = $output[8];
13    $items     = preg_split("/\s+/",$resultRow);
14
15    return $items[3] . ' KB';
16  } else {
17    // We now assume the OS is UNIX
18    // Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
19    // This should work on most UNIX systems
20    $pid = getmypid();
21    exec("ps -eo%mem,rss,pid | grep $pid", $output);
22    $output = explode("  ", $output[0]);
23    //rss is given in 1024 byte units
24    return $output[1] * 1024;
25  }
26}
27
28?>