note 102078 deleted from function.memory-get-usage by danbrown

From: Date: Wed, 26 Jan 2011 14:45:40 +0000
Subject: note 102078 deleted from function.memory-get-usage by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176344@lists.php.net to get a copy of this message
Note Submitter: akniep at linklift dot net ---- <?php // needs PHP v5.1 or later due of Exception-handling function memory_get_usage_by_variable( &$variable ) { // this function depends on PHP's own memory_get_usage() if (! function_exists("memory_get_usage")) return false; // variable does not exist! // Attention: also null variables (uninitalized objects) may be matched by the following condition (even though, they use memory)! if (! isset($variable)) return 0; // usually, PHP-resources are smaller (e.g. file-resources use between 1000 and 2000 bytes; database-connections often below 1000 bytes), // but simply using 5KB does the job for me, since, it is something like an upper bound by my experience // use a value between 1000 and 2000 if you want to use an average value if (is_resource($variable)) return 5000; // saving the current memory-usage $mem_usage_before = memory_get_usage(false); // duplicating the array will create an array of very similar size to the original if (is_array($variable)) { // needs PHP v5.1.0 or later try { // Attention: For very complicated object-structures inside of arrays, the following code may change and immensly blow up the array-size $variable_copy = serialize($variable); $variable_copy = unserialize($variable_copy); } // some objects in PHP are not serializable catch (Exception $ex) // exception is ignored, we assume that some array-element was not serializable { $variable_copy = $variable + array(); } //try-catch } elseif (is_object($variable)) { if ($variable instanceof Closure) // closure-functions since PHP v5.3.0 // Note: variables containing closure-functions do not use much memory, // the necessary memory is already reserved when loading and parsing the file containing the closure-function $variable_copy = new ReflectionFunction( $variable ); // ReflectionFunction, available since PHP 5 else // Attention: for all I know, cloning should work for all objects except closure-functions, // however, having a non-clonable object at this point, may stop the PHP-process with a fatal error! $variable_copy = clone $variable; } // closure-functions are already dealed with as objects, before elseif ( (is_callable($variable)) && (! is_array($variable)) // e.g. array("ReflectionFunction", "export") is callable (this condition is obsolete) ) { // functions created "on-the-fly" with create_function() usually get internally named as follows: <NULLBYTE> . "lambda_" . <integer> if (preg_match( '@^\x0lambda_\d+$@D', $variable )) // if (0 === strpos((string) $variable, chr(0) . "lambda_")) // Note: an instance of ReflectionFunction usually uses less memory than creating a lambda-function with $variable_copy = new ReflectionFunction( (string) $variable );create_function()! // the default case: the string "memory_get_usage" is callable in PHP, but does not use more memory than the string "memory_get_usage" else $variable_copy = ((string) $variable) . ""; } else // if (is_scalar($variable)) $variable_copy = ((string) $variable) . ""; // string, integer, boolean, float, double // how much memory was used to create a copy of the delivered variable? $mem_usage_of_copy = memory_get_usage(false) - $mem_usage_before; // catching strange behaviour if (0 > $mem_usage_of_copy) return false; else return $mem_usage_of_copy; } //memory_get_usage_by_variable() # # ==================================== ==================================== # ==================================== ==================================== # Examples: $string = "A String with 28 characters."; echo memory_get_usage_by_variable( $string ) . "\n"; // usually outputs something like 144 $string = str_repeat("x", 10000); echo memory_get_usage_by_variable( $string ) . "\n"; // usually outputs something like 10120 ?>

« previous php.notes (#176344) next »