1. Home
  2. 站長資源
  3. 站長源碼

WordPress前台顯示內存佔用&負載情況

通過在Wordpress主題目錄下面的functions.php文件中添加函數可以顯示當前WOrdpress記憶體占用和負載的情況,可以更方便的管理wordpress的負載情況。

顯示效果:

WordPress前台顯示內存佔用&負載情況

總物理記憶體:

function memTotal() {
    $str = @file("/proc/meminfo");
    $str = implode("", $str);
    preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
    preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
    $memTotal = round($buf[1][0]/1024, 2);
    return $memTotal;
}

記憶體使用:

function memUsed() {
    $str = @file("/proc/meminfo");
    $str = implode("", $str);
    preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
    preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
    $memTotal = round($buf[1][0]/1024, 2);
    $memFree = round($buf[2][0]/1024, 2);
    $memUsed = $memTotal-$memFree;
    return $memUsed;
}

真實記憶體使用:

function memRealUsed() {
    $str = @file("/proc/meminfo");
    $str = implode("", $str);
    preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
    preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
    $memTotal = round($buf[1][0]/1024, 2);
    $memFree = round($buf[2][0]/1024, 2);
  $memCached = round($buf[3][0]/1024, 2);
  $memBuffers = round($buffers[1][0]/1024, 2);
    $memRealUsed = $memTotal-$memFree-$memCached-$memBuffers;
    return $memRealUsed;
  
}

系統負載:

function loadAvg() {
$str = @file(「/proc/loadavg」);
$str = explode(」 「, implode(「」, $str));
$str = array_chunk($str, 4);
$loadAvg = implode(」 「, $str[0]);
return $loadAvg;
}

函數引用方式示例:
echo memTotal(); // 總共物理記憶體

原创文章,作者:然星,如若转载,请注明出处:https://gov.com.sb/wordpress-top.html

發佈回覆

你的電郵地址並不會被公開。 必要欄位標記為 *

Contact Us

電話:+1 909 475 7355

郵件:govsite@protonmail.ch

尊重中美法律法規
如遇版權法律問題删帖請聯繫郵箱