I wrote my own version of memory_get_usage() if that function is not available:

if (!function_exists('memory_get_usage')) {
	/**
	 * Determine the amount of memory you are allowed to have
	 *
	 * @access public
	 * @return long
	 * @link http://us2.php.net/manual/en/function.memory-get-usage.php#64156
	 * @see link regarding usage of memory_get_usage() homegrown function
	 * @see DBActionPerformer
	 */
	function memory_get_usage() {
		// NEW 1/15/2007: EMERGENCY PATCH TO REINTRODUCE DBActionPerformer
		if (!class_exists('DBActionPerformer')) {
		 global $basePath;
		 if (!class_exists('MethodGeneratorForActionPerformer')) 
		  require_once("$basePath/include/classes.inc.php");
		 require_once("$basePath/include/db_action.inc.php");
		}
		$dbAP =& new DBActionPerformer();		// NO DB ACTION REQUIRED HERE JUST THE CLASS OBJECT INSTANCE FOR getKommandOSArray() METHOD
		list($memKommand, $memRedirect) = @array_values($dbAP->getKommandOSArray('allowed-memory'));
		$outputArray = array();
		exec("$memKommand $memRedirect", $outputArray);
		if ($_ENV['windir'] || $_SERVER['windir']) {
		 return preg_replace( '/[\D]/', '', $outputArray[5]) * 1024;
		} else {
	         $outputArray = @explode('  ', $outputArray[0]);
		 return $outputArray[1] * 1024;
		}
  	 }
}

This function works in *nix and Win XP Pro and Win 2003 Server with no problems.

However, the dev environment is using Win XP Home with PHP 5.2.0 and the function is not only not available, my homegrown version breaks as well because "tasklist" is a DOS program only available to Win XP Pro and Win 2003 Server, definitely not to XP Home.

Because of this I need another variation of my function but don't have any idea where to begin to look. "pslist" is also not an option because the business requirement for the app is that it must be a dual-functioning (Windows and *nix) PHP PORTABLE web application, so you have to pack it up and go every time, so having executables along like "pslist" is not always a viable option.

Suggestions?

Thanx
Phil

    The above function does nothing even vaguely similar to memory_get_usage.

    memory_get_usage tells you how much memory is used by the current instance of PHP. This excludes memory used by the OS, native memory and memory used by other PHP instances in the same process.

    The above will give wildly misleading results, as it includes all of those things and other fluff.

    There is no substitute for memory_get_usage - simply build PHP with --enable-memory-limit as indicated in the docs.

    Mark

      How interesting that the <a href="http://www.php.net">PHP Manual</a> says otherwise.

      But I digress

      I'm sorry but I have to have a substitute because:

      1) The business requirements are dead clear: must work in PHP 4.1.2 minimum (even if there is that 1 person on earth that does)

      2) I am testing right now in PHP 5.2.0 --enable-memory-limit and function_exists('get_memory_usage') returns FALSE unless I use my function

      3) I am using CLI PHP

      Phil

        I don't see how you can achieve the same thing. There is not an obvious relationship between the RSS or VM size of a process in your OS, and the amount of memory used by a PHP script (Although in some cases they may have some relationship).

        The manual says no such thing, it's just a user-contributed note, which is not written by or endorsed by the PHP authors.

        If you must make your application work with 4.1.2, you must do without get_memory_usage. Can you not disable the feature of your application which requires it?

        Mark

          Write a Reply...