Php memory usage code-PHP source code

Source: Internet
Author: User
Ec (2); helps code optimization by detecting memory usage of scripts. PHP provides a garbage collector and a very complex memory manager. The amount of memory used for script execution is increased or decreased. To get the current memory usage, we can use the memory_get_usage () function. To obtain the maximum memory usage at any time point, you can use the memory_limit () function. Viewsourceprint? 01 echo & quot; I script ec (2); script

By detecting the memory usage of the script, the code is optimized. PHP provides a garbage collector and a very complex memory manager. The amount of memory used for script execution is increased or decreased. To get the current memory usage, we can use the memory_get_usage () function. To obtain the maximum memory usage at any time point, you can use the memory_limit () function. View sourceprint? 01 echo "Initial:

The Code is as follows:

". Memory_get_usage ()." bytes ";

02/* prints

03 Initial: 361400 bytes

04 */

05

06 // let's use up some memory

07 for ($ I = 0; I I <100000; $ I ++ ){

08 $ array [] = md5 ($ I );

09}

10

11 // let's remove half of the array

12 for ($ I = 0; I I <100000; $ I ++ ){

13 unset ($ array [$ I]);

14}

15

16 echo "Final:". memory_get_usage (). "bytes ";

17/* prints

18. Final: 885912 bytes

19 */

20

21 echo "Peak:". memory_get_peak_usage (). "bytes ";

22/* prints

23 Peak: 13687072 bytes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.