- Echo memory_get_usage ();
- $ Var = str_repeat ("phpzixue.cn", 10000 );
- Echo memory_get_usage ();
- Unset ($ var );
- Echo memory_get_usage ();
- ?>
-
Output: 62328 122504 62416 description: the output value of the memory_get_usage () function is bytes. 2. format the memory_get_usage () output.
- Function convert ($ size ){
- $ Unit = array ('B', 'KB', 'mb', 'GB', 'TB', 'petab ');
- Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
- }
- Echo convert (memory_get_usage (true ));
- ?>
-
Output: 256 kb 3. the user-defined function obtains the array or variable value size.
- Function array_size ($ arr ){
- Ob_start ();
- Print_r ($ arr );
- $ Mem = ob_get_contents ();
- Ob_end_clean ();
- $ Mem = preg_replace ("/\ n +/", "", $ mem );
- $ Mem = strlen ($ mem );
- Return $ mem;
- }
- $ MemEstimate = array_size ($ GLOBALS );
- ?>
Note: to reduce memory usage, you can use the PHP unset () function to delete unnecessary variables. Similar to the function: PHP mysql_free_result (), you can clear the result set of a database query that is no longer needed, so that more available memory can be obtained. PHP memory_get_usage () can also have a parameter $ real_usage with a Boolean value. The default value is FALSE, indicating that the obtained memory usage does not include the memory occupied by this function (PHP memory manager). when it is set to TRUE, the obtained memory includes this function (PHP memory manager) memory used. In php programming, you can use PHP memory_get_usage () to compare the memory usage of each method, and then select a method with better performance. |