• php查询内存信息操作示例


    转自: https://www.jb51.net/article/160930.htm

    本文实例讲述了php查询内存信息操作。分享给大家供大家参考,具体如下:

    php查询内存信息,是为了更好的查看内存使用情况,更好的优化代码。

    查看当前内存使用情况使用:memory_get_usage()函数。

    查看内存使用峰值:memory_get_peak_usage()函数。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <?php
    header("Content-Type:text/html;charset=utf-8");
    /**
     * 格式化字节大小
     * @param number $size   字节数
     * @param string $delimiter 数字和单位分隔符
     * @return string      格式化后的带单位的大小
     */
    function format_bytes($size, $delimiter = '') {
      $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
      for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
      return round($size, 2) . $delimiter ." ".$units[$i];
    }
    echo "内存初始状态:".format_bytes(memory_get_usage());
    echo "<hr/>";
    echo "开始使用内存<br/>";
    //使用内存
    for($i = 0;$i < 100000;$i++){
      $array[] = md5($i);
    }
    echo "内存状态:".format_bytes(memory_get_usage())."<br/>";
    echo "删除一半的内存<br/>";
    //删除一半的内存
    for($i = 0;$i < 100000;$i++){
      unset($array[$i]);
    }
    echo "最终内存状态:".format_bytes(memory_get_usage());
    echo "<hr/>";
    echo "内存峰值状态:".format_bytes(memory_get_peak_usage());
  • 相关阅读:
    remove white space from read
    optimize the access speed of django website
    dowload image from requests
    run jupyter from command
    crawl wechat page
    python version 2.7 required which was not found in the registry windows 7
    health
    alternate rows shading using conditional formatting
    word
    【JAVA基础】static 关键字
  • 原文地址:https://www.cnblogs.com/maidongdong/p/12712730.html
Copyright © 2020-2023  润新知