Get server memory and CPU usage in PHP Script (RAM & CPU Usage)

Today, We want to share with you get server memory and CPU usage in PHP.In this post we will show you Current Load and Disk Usage with PHP Script, hear for RAM & CPU Usage PHP Script we will give you demo and example for implement.In this post, we will learn about Restart Server from Linux (Ubuntu) with an example.

How to get server memory and CPU usage in PHP?

Here We are Learning to All about php get cpu usage windows, get system info, get load, cpu usage high, system monitor, see what’s using memory, Current Load as well as Disk Usage on Specify Server, load average or many more.

Read Also:  Laravel Database Joins Query Builder

simple first of all you can The first php function will return the Server Memory Usage:
get server memory usage

function get_server_memory_usage(){

    $disk_free_memory = shell_exec('free');
    $disk_free_memory = (string)trim($disk_free_memory);
    $disk_free_memory_arr = explode("\n", $disk_free_memory);
    $all_memory = explode(" ", $disk_free_memory_arr[1]);
    $all_memory = array_filter($all_memory);
    $all_memory = array_merge($all_memory);
    $total_memory_usage = $all_memory[2]/$all_memory[1]*100;

    return $total_memory_usage;
}

echo "<pre>";
print_r(get_server_memory_usage());
echo "<pre>";

simple first of all you can The first php function will return the Server CPU Usage:
get server CPU usage

function get_server_cpu_usage(){

    $all_cpu_load = sys_getloadavg();
    return $all_cpu_load[0];

}

echo "<pre>";
print_r(get_server_cpu_usage());
echo "<pre>";

Get memory size

Get memory size using this php code:

$disk_free_memory = memory_get_usage();

Specify memory unit

Specify memory unit using this php code:

$memory_unit = array(‘Bytes’,’KB’,’MB’,’GB’,’TB’,’PB’);

Display memory size into kb, mb etc.

Display memory size into kb, mb etc. using this php code:

echo ‘All Used Memory : ‘.round($disk_free_memory/pow(1024,($x=floor(log($disk_free_memory,1024)))),2).’ ‘.$memory_unit[$x].”\n”;

Complete code to get server memory and CPU usage in PHP:

here To get server Memory as well as CPU usage in PHP just use the php source code below:

Read Also:  Full form of Technologies Related

<?php
// Get memory size
$disk_free_memory = memory_get_usage();

// Specify memory unit
$memory_unit = array('Bytes','KB','MB','GB','TB','PB');

// Display memory size into kb, mb etc.
echo 'All Used Memory : '.round($disk_free_memory/pow(1024,($x=floor(log($disk_free_memory,1024)))),2).' '.$memory_unit[$x]."\n";
?>

I hope you get an idea about PHP gets real-time CPU memory usage.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.