- Echo memory_get_usage (),'
'; // 313864
- $ Tmp = str_repeat ('http: // www.nowamagic.net/", 4000 );
- Echo memory_get_usage (),'
'; // 406048
- Unset ($ tmp );
- Echo memory_get_usage (); // 313952
- ?>
-
The comments behind the above program represent their output (in bytes (s), that is, the memory used by the PHP script at that time (excluding the memory occupied by the memory_get_usage () function itself ). The preceding example shows that to reduce memory usage, you can use the PHP unset () function to delete unnecessary variables. Similarly, the PHP mysql_free_result () function can be used to clear the result set of a database query that is no longer needed, so that more available memory can be obtained. The PHP memory_get_usage () function 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). if it is set to TRUE, the resulting memory does not include the memory occupied by this function (PHP memory manager. In actual programming, you can use the memory_get_usage () function to compare the memory usage of each method and select the method that occupies a small amount of memory. Paste a practical function:
- If (! Function_exists ('memory _ get_usage '))
- {
- /**
- + ----------------------------------------------------------
- * Obtain memory usage
- + ----------------------------------------------------------
- * @ Return integer
- + ----------------------------------------------------------
- */
- Function memory_get_usage ()
- {
- $ Pid = getmypid ();
- If (IS_WIN)
- {
- Exec ('tasklist/FI "PID eq '. $ pid.'"/FO list', $ output );
- Return preg_replace ('/[^ 0-9]/', ', $ output [5]) * 1024;
- }
- Else
- {
- Exec ("ps-eo % mem, rss, pid | grep $ pid", $ output );
- $ Output = explode ("", $ output [0]);
- Return $ output [1] * 1024;
- }
- }
- }
Another example of using a function:
- // Memory_get_usage ();
- $ M1 = memory_get_usage ();
- Echo'
M1: ', $ m1; // 58096
- $ A = 'hello ';
- $ B = str_repeat ($ a, 1000 );
- $ M2 = memory_get_usage ();
- Echo'
M2: ', $ m2; // 63424
- Unset ($ B );
- $ M3 = memory_get_usage ();
- Echo'
M3: ', $ m3; // 58456
- ?>
|