php 查看使用多少内存

方法memory_get_usage 是指当前脚本正在使用的内存
unset只是把内存标记为空闲但并没有释放,要GC程序结束后才会释放

$bytes = memory_get_peak_usage();

function formatBytes($bytes, $precision = 2) {
    $units = array("b", "kb", "mb", "gb", "tb");

    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);

    $bytes /= (1 << (10 * $pow));

    return round($bytes, $precision) . " " . $units[$pow];
}

echo formatBytes($bytes);

猜你喜欢

转载自www.cnblogs.com/fuqian/p/10629646.html